python里实现字符串替换

简介:
1
2
3
4
5
6
7
8
9
10
>>>aa = "hi"
>>>num = [ 0 , 1 , 2 , 3 , 4 , 5 ]
>>> for  in  num :
             if  i > 0 :
                 print (aa)
hi
hi
hi
hi
hi

上面这个就是实现“把某个字符串,反复出现某list里满足条件的元素次数”,这个得到的结果是一个str,如果要简化一下这段代码如下:

1
2
>>>[aa  for  in  num  if  i > 0 ]
[hi,hi,hi,hi,hi]

这里一定要有中括号,不然的话算SyntaxError: invalid syntax。得到的结果是一个list。


replace函数

一般情况下来说,对于字符串的替换,常用的都是.replace函数。replace()可以设定替换次数。

1
2
3
4
5
6
7
8
9
10
>>> aaa = "wade,james,kobe,lewis,kevin"
>>> s = aaa.replace( "james" , "curry" )
'wade,curry,kobe,lewis,kevin'
>>> type (s)
< class  'str' >
>>> champions = 'warriors,spurs,heat,heat,mavericks,lakers,lakers,celtics,spurs'
>>> champions.replace( "spurs" , "马刺队" , 1 )     #只替换一次
'warriors,马刺队,heat,heat,mavericks,lakers,lakers,celtics,spurs'
>>> champions.replace( "spurs" , "马刺队" , 2 )     #替换两次
'warriors,马刺队,heat,heat,mavericks,lakers,lakers,celtics,马刺队'

rm.sub模块

sub模块主要是字符串的针对性替换作用,在这一点上sub()和replace()的功能类似的,但是sub()支持正则表达式。使用格式是text.sub(r"要替换的内容","替换成的内容",替换范围),举个例子:

1
2
3
4
5
>>> import  re
>>>s0 = "james and wade"
>>>s1 = re.sub(r "james" , "bosh" ,s0)         #r指的是原生字符串的意思
>>> print (s1)
bosh  and  wade


sub模块支持 | 的引用,比如把上面代码修改一下:

1
2
3
4
5
>>> import  re
>>>s0 = "james and wade"
>>>s2 = re.sub(r "james|wade" , "bosh" ,s0)     # | 就是或的意思
>>> print (s2)
bosh  and  bosh

但是要注意,如果类似于linux里的或,用[]括起来的话,结果可大不相同:

1
2
3
4
5
6
>>> import  re
>>>s0 = "james and wade"
>>>s3 = re.sub(r "[james|kobe]" , "bosh" ,s0)     # 这里原来打算把james或者kobe替换成bosh
>>> print (s3)
boshboshboshboshbosh boshnd wboshdbosh  
#可以看出只要是 j a m e s k o b e这几个字母就都变成了bosh,而出现了boshnd,是应为n和d 不属于上面几个字母,所以nd保留了下来。

如果要把每一项都改,可以用[a-z],

1
2
3
4
5
>>> import  re
>>>s0 = "james and wade"
>>>s4 = re.sub(r "[a-z]" , "bosh" ,s0)
>>> print (s4)
boshboshboshboshbosh boshboshbosh boshboshboshbosh

那么对于数字而言,就是[1-9],但是有没有一种方式,把所有的元素都包括了呢?有的,那就是\w

1
2
3
4
5
>>> import  re
>>>s0 = "james23 and wade3"     #这是一个混杂型的字符串,里面有数字、字母
>>>s5 = re.sub(r "\w" , "bosh" ,s0)
>>> print (s5)
boshboshboshboshbosh boshboshbosh boshboshboshbosh

\w对于“@#¥%^$”特殊符号是不好使的。


re还有一个subn的模块,再看看这个。

1
2
3
4
5
6
7
8
9
10
>>>  import  re
>>> s = "1abc23def45"
>>>  print ( str (re.subn(r "\w" , "hi" ,s)[ 1 ]))
11                             #这个是体现了一共替换了多少次,
>>>  print ( str (re.subn(r "\w" , "hi" ,s)[ 0 ]))
hihihihihihihihihihihi         #只有[0]和[1],[0]是替换的内容。
>>>  print ( str (re.subn(r "\w" , "hi" ,s)))
( 'hihihihihihihihihihihi' 11 )
>>>  print ( str (re.subn(r "[a-z]" , "hi" ,s)))
( '1hihihi23hihihi45' 6 )


对于字符串的基本操作:http://blog.csdn.net/minsenwu/article/details/7891453



 本文转自 苏幕遮618 51CTO博客,原文链接:http://blog.51cto.com/chenx1242/1767268

相关文章
|
1天前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
1天前
|
Python
Python字符串和字节不要混淆str.format()和bytes.format()
【5月更文挑战第6天】Python字符串和字节不要混淆str.format()和bytes.format()
4 1
|
1天前
|
Python
Python字符串和字节使用正确的编码/解码
【5月更文挑战第6天】Python字符串和字节使用正确的编码/解码
6 2
|
1天前
|
存储 Python
python字符串和字节明确数据类型
【5月更文挑战第6天】python字符串和字节明确数据类型
6 2
|
2天前
|
Python
Python避免在字符串和字节之间混淆
【5月更文挑战第5天】Python避免在字符串和字节之间混淆
13 3
|
4天前
|
数据安全/隐私保护 开发者 Python
【Python 基础】检查字符串是否只包含数字和字母?
【5月更文挑战第8天】【Python 基础】检查字符串是否只包含数字和字母?
|
4天前
|
Python
【Python 基础】如何将一个字符串转化为全大写和全小写?
【5月更文挑战第8天】【Python 基础】如何将一个字符串转化为全大写和全小写?
|
4天前
|
机器学习/深度学习 存储 人工智能
python 字符串的三种定义方式
python 字符串的三种定义方式
10 1
|
6天前
|
Python Perl
Python中的字符串分析:判断字符串中是否包含字母
Python中的字符串分析:判断字符串中是否包含字母
10 0
|
6天前
|
C语言 Python
【Python 基础】如何进行字符串插值?
【5月更文挑战第6天】【Python 基础】如何进行字符串插值?