Python replace() 和 re.sub() 字符串字符替換
replace()
testStr = 'aa:bb[cc'
testStr.replace(':','_')
每次只能替換一個字符或字符串
re.sub()
import re
testStr = 'aa:bb[cc}'
把 :[} 替換成 _
re.sub(r'[:[}]', '_', testStr)
re.sub() 的第一個參數是pattern,使用正則表達式,所以例子中 r'[:[}]' 代表 [] 中的任何一個字符,更多使用請另外學習正則表達式
