Python 字符串操作 starswitch() find() re.IGNORECASE replace() join()

检测开头&结尾
开头:startswith()
url = 'http://www.python.org'

url.startswith('http')

>>>True

结尾:endswith()


url = 'http://www.python.org'


url.endswith('org')


>>>True

筛选多个结果

url = 'http://www.python.org'

choices = ('ogr', 'com')

url.endswith(choices)

>>>True

 

寻找字符串:find()
string = "I am Etisan"

string.find("am")

>>>2

string.find("are")

>>>-1

忽略大小写:re.IGNORECASE

#导入re模块

import re

re.findall('i',string,flags=re.IGNORECASE)

>>>['I', 'i']

 

 

替换:replace()
string.replace('Etisan','ET')

>>>'I am ET'

忽略大小写

re.sub('Etisan','ET',string,flags=re.IGNORECASE)

>>>'I am ET'

 

合并拼接:join()
parts = ['I', 'am', 'Etisan']

''.join(parts)

>>>'IamEtisan'

' '.join(parts)

>>>'I am Etisan'

转载于:https://www.cnblogs.com/BeyondTechnology/p/9904062.html

你可能感兴趣的:(Python 字符串操作 starswitch() find() re.IGNORECASE replace() join())