#查找某个字符串
string = "I am KEN"
string.find("am")
string.find("boy")
#忽略大小写的搜索
import re
text = 'UPPER PYTHON, lower python, Mixed Python'
re.findall('python', text, flags=re.IGNORECASE)
#搜索和替换
text = 'yeah, but no, but yeah, but no, but yeah'
text.replace('yeah', 'yep')
#忽略大小写的替换
text = 'UPPER PYTHON, lower python, Mixed Python'
re.sub('python', 'snake', text, flags=re.IGNORECASE)
#合并拼接字符串
parts = ['Is', 'Chicago', 'Not', 'Chicago?']
' '.join(parts)
'Is Chicago Not Chicago?'
','.join(parts)
'Is,Chicago,Not,Chicago?'
''.join(parts)
'IsChicagoNotChicago?'
查找,替换,以及合并拼接字符串
最新推荐文章于 2022-10-09 13:25:10 发布