首页 > 代码库 > python字符串功能学习
python字符串功能学习
1、center
name = ‘Colum‘print(name.center(18,‘*‘))
View Code
将内容填入*的中间,效果为******Colum*******
2.count
计算输入参数的数量
name = ‘Colum‘print(name.count(‘u‘))
3.decode 解码 encode 编码
4.expandtabs
将tab变为空格,也可以用\t 替换空格实现
name = ‘He is 2*tab‘print(name.expandtabs())
He is 2*tab
5.find index
查找参数,返回位置。find返回失败也是有值-1, index查找 失败返回错误
name = ‘He is 2*tab‘print(name.find(‘2‘))返回 7
print(name.find(‘3‘))
-1
print(name.index(‘3‘))
ValueError: substring not found
6.join
连接字符串
name = ‘He is 2*tab‘print("_".join(name))H_e_ _i_s_ _ _2_*_t_a_b
7.partition
将内容进行分割三部分,以参数为中间
name = ‘He is 2*tab‘print(name.partition(‘2‘))(‘He is\t\t‘, ‘2‘, ‘*tab‘)
8.replace
替换
name = ‘He is 2*tab‘print(name.replace(‘2‘,‘3‘))He is 3*tab
9.split
分割返回列表,默认空格分割
name = ‘He is 2*tab‘print(name.split(‘*‘))[‘He is\t\t2‘, ‘tab‘]
10.strip
默认去除行两头的空格,参数为去除内容
name = ‘22112 He is 2*tab 21 2‘print(name.strip(‘2‘))112 He is 2*tab 21
python字符串功能学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。