2017-07-02 22 views

回答

0

str.translate不適合用於此特定用例的正確功能。它所做的就是用另一個字符替換單個字符。它不適用於字符組。例如:

>>> string = 'The quick brown fox and a hound' 
>>> tab = str.maketrans('The', '123') 
>>> string.translate(tab) 
'123 quick brown fox and a 2ound' 

除了翻譯'The',它還在'hound'翻譯'h'

爲您的特定使用案例,str.replace將是一個很好的選擇:

>>> string.replace('The', '123') 
'123 quick brown fox and a hound' 
相關問題