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变为空格,也可以用 替换空格实现

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		', '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		2', 'tab']

10.strip

默认去除行两头的空格,参数为去除内容

name = '22112   He is        2*tab  21  2'
print(name.strip('2'))

112   He is        2*tab  21  
原文地址:https://www.cnblogs.com/jonyq/p/5922135.html