Python清除字串中的某些資料

2021-10-01 17:56:16 字數 629 閱讀 2051

def

clear

(data, l)

:"""

清除字串中的某些資料

:param data: str, 待清除的字串

:param l: str, 某個資料. list, 一批資料

:return: str, 清除完畢的字串

"""ifisinstance

(l,list):

for i in l:

data = data.replace(i,"")

ifisinstance

(l,str):

data = data.replace(l,"")

return data

data1 =

'請問您**不舒服?'

l1 =

["請問"

,"您"

,"?"

]data2 =

'你不知道我不知道他不知道'

l2 =

'不知道'

print

(clear(data1, l1)

)print

(clear(data2, l2)

)# **不舒服

# 你我他

python中的字串

方法1 用字串的join方法 a a b c d content content join a print content 方法2 用字串的替換佔位符替換 a a b c d content content s s s s tuple a print content 我們可以通過索引來提取想要獲取的...

python中的字串

b nihao hahah xixi 輸出 nihao nhahah nxixi n 原字串 big r this hhaha big輸出 this nhhaha 還原為unicode字串 hello u hello u0020world hello輸出 hello world 字串是不可以改變的 ...

python中的字串

字串連線操作 字串複製操作 字串索引操作,通過索引訪問指定位置的字元,索引從0開始 字串取片操作 完整格式 開始索引 結束索引 間隔值 結束索引 從開頭擷取到結束索引之前 開始索引 從開始索引擷取到字串的最後 開始索引 結束索引 從開始索引擷取到結束索引之前 擷取所有字串 開始索引 結束索引 間隔值...