Python攻克之路-字符串
字符串
字符串是以单引号'或者双引号"引起来的任意文本,如'abc','123'等等
注意:单引号或者双引号只是一种表示方式,不是字符串的一部分,字符串'abc'只有a,b,c这3个字符。如果单引号本身是一个字符,那就可以使用双引号括起来,如"I'm ok"包含的字符是I,',m,空格,o,k,这6个字符
创建字符串:
In [1]: a="I'm ok" In [2]: a Out[2]: "I'm ok"
字符串对应操作:
重复输出 可以让它打印两次的hello
In [3]: print('hello'*2)
hellohello
[:]通过索引获取字符串,这里和列表切片操作是相同的
In [4]: print('helloworld'[2:])
lloworld
in 成员运算符,如果字符串中包含给定的字符返回true
In [5]: print('el' in 'hello')
True
% 格式字符串
In [6]: print('Reid is a man')
Reid is a man
In [7]: print('%s is a good teacher '%' reid') # 这里可以列灵活,不用硬编码
reid is a good teacher
+字符串拼接
In [20]: a='123' In [21]: b='abc' In [22]: c=a+b #这种操作,效率不太好,在开辟更多的内存空间 In [23]: c Out[23]: '123abc'
join方法进行拼接,是字符串的一个内置方法
''.join([a,b]) #这种方法的效率高,拼接通过前面的字符串''里面可以 In [20]: a='123' In [21]: b='abc' In [24]: ''.join([a,b]) Out[24]: '123abc' In [25]: '---'.join([a,b]) Out[25]: '123---abc' In [27]: d='44' In [28]: c='++++'.join([a,b,d]) In [29]: c Out[29]: '123++++abc++++44'
python中的内置方法
In [1]: st='hello reid'
count统计元素个数****
In [2]: st.count('l')
Out[2]: 2
capitalize首字母大写
In [3]: st.capitalize() Out[3]: 'Hello reid'
center居中
In [4]: st.center(50,'-') Out[4]: '--------------------hello reid--------------------'
endswith判断是否以某个内容结尾
In [6]: st.endswith('d')
Out[6]: True
startswith判断是否以某个内容开头****
In [7]: st.startswith('he')
Out[7]: True
expandtabs设置tab的空格
In [8]: st='he\tllo reid' In [9]: st.expandtabs(tabsize=10) Out[9]: 'he llo reid'
find查找到第一个元素并返回相应的索引值 ****
In [10]: st='hello reid'
In [11]: st.find('r')
Out[11]: 6
format格式化输出****
In [12]: st='hello reid {name} is {age}'
In [13]: st.format(name='lync',age=49)
Out[13]: 'hello reid lync is 49'
In [15]: st.format_map({'name':'you','age':90}) #以字典的形式赋值
Out[15]: 'hello reid you is 90'
index与find一样查看返回索引,但是index查找不到的会有报错
In [16]: st.index('r')
Out[16]: 6
判断是否是数字或字母,汉字也可以,否则特殊字符返回false
In [18]: str='abc123' In [19]: str.isalnum() Out[19]: True In [20]: str='abc123*' In [21]: str.isalnum() Out[21]: False
decimal判断是否十进制
In [22]: str='10101' In [23]: str.isdecimal() Out[23]: True
isdigit判断是否是整型
In [1]: str='1234' In [2]: str.isdigit() Out[2]: True In [3]: str='1234.33' In [4]: str.isdigit() Out[4]: False
isidentifier检验是否非法变量
In [5]: str='33abc' In [6]: str.isidentifier() Out[6]: False In [7]: str='abc' In [8]: str.isidentifier() Out[8]: True
islower判断是否是全小写
In [9]: str='abc' In [10]: str.islower() Out[10]: True In [11]: str='Abc' In [12]: str.islower() Out[12]: False
isupper判断是否是全大写
In [13]: str='ABCE' In [14]: str.isupper() Out[14]: True
isspace判断是否是空格
In [15]: str=' ' In [16]: str.isspace() Out[16]: True In [17]: str=' 9' In [18]: str.isspace() Out[18]: False
istitle判断是否是标题,每个单词的首字母是大写的
In [19]: str='My Title' In [20]: str.istitle() Out[20]: True In [21]: str='My title' In [22]: str.istitle() Out[22]: False
lower把大写转换成小写****
In [23]: str='My title' In [24]: str.lower() Out[24]: 'my title'
upper把小写转换成大写****
In [25]: str.upper() Out[25]: 'MY TITLE'
swapcase把小写转换成大写,大写转换成小写
In [26]: str='My TItle' In [27]: str.swapcase() Out[27]: 'mY tiTLE'
ljust把位置移至左边
In [28]: str='My TItle' In [29]: str.ljust(50,'*') Out[29]: 'My TItle******************************************'
rjust把位置移至右边
In [30]: str.rjust(50,'*') Out[30]: '******************************************My TItle'
strip把字符串开头或结尾的空格或者换行符去掉****(文本操作时,结尾一般有换行符,处理时可以先把它去掉)
In [33]: str=' My TItle' In [34]: str.strip() #去左右 Out[34]: 'My TItle' In [38]: str.lstrip() #去左边 Out[38]: 'My TItle' In [39]: str.rstrip() #去右边 Out[39]: ' My TItle'
replace替换:默认是替换全部****
In [40]: str='My title'
In [41]: str.replace('My','Our')
Out[41]: 'Our title'
In [42]: str='My title My'
In [43]: str.replace('My','Our')
Out[43]: 'Our title Our'
In [44]: str.replace('My','Our',1) #控制替换的次数
Out[44]: 'Our title My'
rfind查找索引
In [45]: str='My title My'
In [46]: str.rfind('t') #从右往左找,但是是真实索引位置
Out[46]: 5
In [47]: str.rfind('y')
Out[47]: 10
split对字符串做一个做分隔****
In [48]: str='My title My'
In [49]: str.split() #从左往右
Out[49]: ['My', 'title', 'My'] #存储到列表里
In [50]: str=' '.join(['My', 'title', 'My']) ##把列表变成字符串使用join
In [51]: str
Out[51]: 'My title My'
In [52]: str='My title My'
In [53]: str.rsplit('i',1) #以右为准,只分隔一次
Out[53]: ['My t', 'tle My']
title按标题的格式来写
In [54]: str='my title my' In [55]: str.title() Out[55]: 'My Title My'

浙公网安备 33010602011771号