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

检测开头&结尾

开头:starswitch()

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

url.starswich('http')

>>>True

结尾:endswitch()

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

url.endswich('org')

>>>True

筛选多个结果

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

choices = ('ogr', 'com')

url.endswich(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'


你可能感兴趣的:(Python)