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

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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-08-10
  • 2021-11-12
  • 2023-04-10
  • 2022-12-23
  • 2023-03-29
相关资源
相似解决方案