python 取代字符串中的某项 replace,split, join

来源:互联网 发布:配置php开发环境 编辑:程序博客网 时间:2024/05/29 08:12

首先需要:import string

然后有两种方法取代字符串中的某项:

方法1:

>>> a='...hello...the....world............'
>>> b=a.replace('.',' ')
>>> print b
  hello  the    world

方法2:

>>> a='...hello...the....world............'
>>> b=string.replace(a,'.',' ')
>>> print b
  hello  the   world


split函数:

>>> print b
 hello  the   world 

>>>list1=b.split()#把空格过滤出去,剩下的组成一个list,也可以在split()的括号里传入一个参数
>>> print list1
['hello', 'the', 'world']

join函数:

>>> list1
['hello', 'the', 'world']
>>> delimiter=' '

>>> line=delimiter.join(list1)
>>> print line
hello the world


0 0
原创粉丝点击