【python】字符串

时间:2023-12-29 21:11:02

>>> str1="welcom to China"
>>> str1[2:4]
'lc'
>>> str1[7]
't'
>>> str1=str1[:8]+"中国"+str1[8:]
>>> str1
'welcom t中国o China'
>>> #本质上是重新创建了一个字符串并贴上str1的标签,原来被str1标签贴的字符串仍存在只是现在未被任何标签贴上,之后会被python自动回收机制收回。
>>> str2='china'
>>> str2.capitalize()
'China'
>>> str2='CHINA is beautiful, HAHA'
>>> str2.casefold()
'china is beautiful, haha'
>>> str2.center(50)
' CHINA is beautiful, HAHA '
>>> #设置字符串长度为50,并剧中显示。
>>> str2.count('in')
0
>>> str2.count('IN')
1
>>> str2.count("I")
1
>>> str2.count('a')
1
>>> str2.count('i')
2
>>> str2
'CHINA is beautiful, HAHA'
>>> str2.count('H')
3
>>> str2.count("H",2)
2
>>> str2.endswith('AHA')
True
>>> str2.endswith("H")
False
>>> str2='I\tlove\tChina'
>>> str2
'I\tlove\tChina'
>>> str2.expandtabs()
'I        love    China'
>>> str2='飞雪连天射白鹿'
>>> str2.find("天")
3
>>> str2.find('a')
-1
>>> str2="中国"
>>> str2.islower()
False
>>> str2.isupper()
False
>>> str2='AbC'
>>> str2.islower()
False
>>> str2.isupper()
False
>>> str2="AB"
>>> str2.isupper()
True
>>>