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

檢測開頭&結尾
開頭:startswith()
url = 'http://www.python.org'python

url.startswith('http')url

>>>True字符串

結尾:endswith()string


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


url.endswith('org')import


>>>Trueim

篩選多個結果co

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

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'

相關文章
相關標籤/搜索