如何去掉字符串中不需要的字符

方法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}

posted @ 2017-03-31 20:32  keven0526  阅读(3082)  评论(0编辑  收藏  举报