方法1


方法2

方法3

改進版本
(因為上面的方法只能替換一個符號)

方法4
兩種 translate()方法

string下的translate()

轉變 a > x b > y c > z
構建映射表

結果


上述 table 若為None 說明不做任何的映射 第二個參數 表示 要刪除字符串的指定的字符

unicode 下的translate()
問題三的解決方法



Python 字典(Dictionary) fromkeys()方法

#!/usr/bin/python
seq = ('name', 'age', 'sex')
dict = dict.fromkeys(seq)
print "New Dictionary : %s" % str(dict)
dict = dict.fromkeys(seq, 10)
print "New Dictionary : %s" % str(dict)
New Dictionary : {'age': None, 'name': None, 'sex': None}
New Dictionary : {'age': 10, 'name': 10, 'sex': 10}