终于还是踏上了Python的不归路,不知道能不能走的完。
先总结一下今天学习的字符串的各个功能吧:只写了部分用的比较多的。
1、capitalize: 字符串首字母大写
>>> name='chaizhenhua'
>>> result=name.capitalize()
>>> print(result)
Chaizhenhua
2、casefold: 大写字母转小写(所有的大写全转为小写)
>>> name='ChAi'
>>> result=name.casefold()
>>> print(result)
chai
3、center: 居中显示,可以制定偏移值和填充字符
>>> result=name.center(20)
>>> print(result)
ChAi
>>> other=name.center(20,'*')
>>> print(other)
********ChAi********
4、count: 统计一个字符串中某个字符的出现的次数
>>> name='chaizhenhua666'
>>> result=name.count('c')
>>> print(result)
1
>>> result=name.count('6')
>>> print(result)
3
>>> result=name.count('hu')
>>> print(result)
1
5、encode: 指定字符的编码格式或者转换字符的编码格式
>>> name='柴振华'
>>> result=name.encode('gbk')
>>> print(result)
b'xb2xf1xd5xf1xbbxaa'
6、endswith: 判断字符串是否以指定字符结尾
>>> name='chaizhenhua'
>>> result=name.endswith('ua')
>>> print(result)
True
>>> result=name.endswith('ua',0,10) # 0表示开始位置即‘c’,10表示结束位置,注意,这里的起止位置是左开右闭区间
>>> print(result)
False
>>> result=name.endswith('ua',0,11)
>>> print(result)
True
7、expandtabs: 制表符转换为空格,默认一个制表符转换为8个空格,可以指定
这个有个特殊的地方是:如果制表符前有超过8个字符,则 转化为8个空格,如果少于8个字符,则 转化为8-x个空格
>>> name=' chai'
>>> print(name.expandtabs().count(' '))
8
>>> name='c hai'
>>> print(name.expandtabs().count(' '))
7
>>> name='ch ai'
>>> print(name.expandtabs().count(' '))
6
>>> name='cha i'
>>> print(name.expandtabs().count(' '))
5
>>> name='chai '
>>> print(name.expandtabs().count(' '))
4
>>> name='chaic '
>>> print(name.expandtabs().count(' '))
3
>>> name='chaich '
>>> print(name.expandtabs().count(' '))
2
>>> name='chaicha '
>>> print(name.expandtabs().count(' '))
1
>>> name='chaichai '
>>> print(name.expandtabs().count(' '))
8
8、find: 查找字符在字符串中所处的位置,存在返回具体index位置,不存在返回-1
>>> name='chai'
>>> print(name.find('a'))
2
>>> print(name.find('ha'))
1
>>> print(name.find('z'))
-1
9、index: 字符索引,find查找字符返回的值就是index,与find不同的是,不存在的值会直接报错,而find是返回-1
>>> name='chai'
>>> print(name.index('h'))
1
>>> print(name.index('z'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
10、format: 字符串拼接或者说传参
>>> name='chai{0}'
>>> print(name.format('zhenhua'))
chaizhenhua
>>> name='chai {0} is a {1}'
>>> print(name.format('zhenhua','student'))
chai zhenhua is a student
>>> name='chaizhenhua is {job}'
>>> print(name.format(job='developer'))
chaizhenhua is developer
11、isalnum: 判断字符串是否由字母或数字组成,是返回True,否返回False
>>> name='chai'
>>> print(name.isalnum())
True
>>> name=' '
>>> print(name.isalnum())
False
>>> name='www.baidu.com'
>>> print(name.isalnum())
False
>>> name='chai123'
>>> print(name.isalnum())
True
12、islower: 判断是否为全部小写字母
>>> name='CHAI'
>>> print(name.lower())
chai
>>> print(name.islower())
False
>>> name='123'
>>> print(name.islower())
False
>>> name='chai'
>>> print(name.islower())
True
13、istitle: 判断是否为标题,即每个单词有且只有首字母大写返回True
>>> name='ChaiZhenHua'
>>> name.istitle()
False
>>> name='Chai Zhen Hua'
>>> name.istitle()
True
>>> name='chai Zhen Hua'
>>> name.istitle()
False
>>> name='Chaihhenhua'
>>> name.istitle()
True
14、isupper: 判断字符串的所有字母都为大写返回True
>>> name='chAi'
>>> name.isupper()
False
>>> name='CHAI'
>>> name.isupper()
True
15、strip: 去除字符串左右的空格; rstrip: 去除右边的空格; lstrip: 去除左边的空格
>>> name=' chai zhen '
>>> name.strip()
'chai zhen'
>>> name.rstrip()
' chai zhen'
>>> name.lstrip()
'chai zhen '
16、partition: 字符串分割,可以指定分割符
>>> name='chaizhenhuaisstudent'
>>> print(name.partition('is'))
('chaizhenhua', 'is', 'student')
17、replace: 字符替换
>>> name='chai'
>>> print(name.replace('c','A'))
Ahai
18、swapcase: 字母大小写转换,大写变小写,小写变大写
>>> name='chai'
>>> print(name.swapcase())
CHAI
19、title: 字符转换为标题,即每个单词的首字母大写
>>> name='chai zhen hua'
>>> print(name.title())
Chai Zhen Hua
20、upper: 字母全部大写
>>> name='chai'
>>> print(name.upper())
CHAI