美文网首页日更成长营自学编程Python
自学Python:字符串处理方法

自学Python:字符串处理方法

作者: 小强聊成长 | 来源:发表于2021-07-20 11:40 被阅读0次

.lower()把字母全部变成小写。

a='Hi,Hello python!'

print(a.lower())

执行结果:

hi,hello python!

.upper()把字母全部变成大写。

a='Hi,Hello python!'

print(a.upper())

执行结果:

HI,HELLO PYTHON!

.split(sep=None)根据sep分割成列表。

a='a,b,c,d'

print(a.split(','))

执行结果:

['a', 'b', 'c', 'd']

也可以是空格

a='a b c d'

print(a.split(' '))

执行结果一样。

.count(sub)统计sub在字符串中出现的次数。

a='happy day'

print(a.count('p'))

执行结果:

2

.replace(old,new)用new的内容替换old的内容。

a='happy day'

print(a.replace('day','new day'))

执行结果:

happy new day

.center(width[,fillchar]根据宽度width值来居中字符串,fillchar内容可以选择是否需要。

a='happy day'

print(a.center(15,'#'))

执行结果:

###happy day###

.strip(chars)去除字符串两侧包含的chars内容

a=' =happy day@'

print(a.strip(' =@'))

执行结果:

happy day

.join()在每个元素后面增加一个指定的内容,最后一个元素除外

print (','.join('abcdef'))

执行结果:

a,b,c,d,e,f
________________END______________

相关文章

网友评论

    本文标题:自学Python:字符串处理方法

    本文链接:https://www.haomeiwen.com/subject/ihpppltx.html