码迷,mamicode.com
首页 > 编程语言 > 详细

python字符串功能学习

时间:2016-09-30 01:16:08      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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字符串功能学习

标签:

原文地址:http://www.cnblogs.com/jonyq/p/5922135.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!