Python字符串处理函数记录

1、 s.strip()
s.strip()的作用是去除前后两边的空白符(包括换行符)

#coding:utf-8
a = "  abc  daaa  "
a = a.strip()
print a

>>>abc  daaa


#结论:s.strip(rm)的作用是去除字符串前后的rm字符
#     s.strip()的作用是去除前后两边的空白符(包括换行符)    

2、s.join(b)
用s将a分隔开来

#coding:utf-8
str_1 = "-"
a = "abcd"

print str_1.join(a)

>>>a-b-c-d

3、s.split()
将字符串按照某字符分开成为list,list内容为子字符串

#coding:utf-8

a  = "ab c d ef"
b = "www.baidu.com"

print a.split()   #不指定即空格作为分隔符
print b.split(".")  #以点作为分隔符

>>>
['ab', 'c', 'd', 'ef']
['www', 'baidu', 'com']

猜你喜欢

转载自blog.csdn.net/liushui94/article/details/78460752