首先需要:import string
然后有两种方法取代字符串中的某项:
方法1:
>>> a='...fuck...the....world............'
>>> b=a.replace('.',' ')
>>> print b
fuck the world
方法2:
>>> a='...fuck...the....world............'
>>> b=string.replace(a,'.',' ')
>>> print b
fuck the world
回顾:
split函数:
>>> print b
fuck the world >>> list1=b.split()#把空格过滤出去,剩下的组成一个list,也可以在split()的括号里传入一个参数
>>> print list1
['fuck', 'the', 'world']
join函数:
>>> list1
['fuck', 'the', 'world']
>>> delimiter=' '>>> line=delimiter.join(list1)
>>> print line
fuck the world