首页 > 代码库 > python字符串功能学习

python字符串功能学习

1、center

name = Columprint(name.center(18,*))

 

View Code

将内容填入*的中间,效果为******Colum*******

2.count

计算输入参数的数量

name = Columprint(name.count(u))

 

3.decode 解码   encode 编码

4.expandtabs

将tab变为空格,也可以用\t 替换空格实现

 

name = He is        2*tabprint(name.expandtabs())

 

He is           2*tab

5.find    index

查找参数,返回位置。find返回失败也是有值-1, index查找 失败返回错误

 

name = He is        2*tabprint(name.find(2))返回 7
print(name.find(‘3‘))
-1

print(name.index(‘3‘))
ValueError: substring not found

 

6.join

连接字符串

 

name = He is        2*tabprint("_".join(name))H_e_ _i_s_    _    _2_*_t_a_b

 

7.partition

将内容进行分割三部分,以参数为中间

name = He is        2*tabprint(name.partition(2))(He is\t\t, 2, *tab)

8.replace

替换

name = He is        2*tabprint(name.replace(2,3))He is        3*tab

9.split

分割返回列表,默认空格分割

name = He is        2*tabprint(name.split(*))[He is\t\t2, tab]

10.strip

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

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

 

python字符串功能学习