如何在python 3.x中使用string.replace()

时间:2012-02-26 09:50:21

标签: python python-3.x

在python 3.x上不推荐使用string.replace()。这样做的新方法是什么?

9 个答案:

答案 0 :(得分:248)

与2.x一样,使用str.replace()

示例:

>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'

另请注意,点的结合比字符串结合更强,i。即使用括号:('Hello'+'world')。replace('world','Guido')

答案 1 :(得分:96)

replace()是python3中<class 'str'>的一种方法:

>>> 'hello, world'.replace(',', ':')
'hello: world'

答案 2 :(得分:7)

python 3中的replace()方法简单地用于:

a = "This is the island of istanbul"
print (a.replace("is" , "was" , 3))

#3 is the maximum replacement that can be done in the string#

>>> Thwas was the wasland of istanbul

# Last substring 'is' in istanbul is not replaced by was because maximum of 3 has already been reached

答案 3 :(得分:3)

试试这个:

mystring = "This Is A String"
print(mystring.replace("String","Text"))

答案 4 :(得分:1)

仅供参考,当将一些字符附加到字符串中的任意位置固定字时(例如,通过添加后缀 -ly 将形容词更改为副词),为了便于阅读,您可以将后缀放在行尾。为此,请在split()内使用replace()

s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'

答案 5 :(得分:1)

str.replacePython 3 的官方文档

官方文档:Python 3's str.replace

<块引用>

str.replace(old, new[, count])

返回字符串的副本,其中所有出现的子字符串 old 都被 new 替换。如果给出了可选参数计数,则仅替换出现的第一个计数。

对应的VSCode的语法说明是:

enter image description here

<块引用>

str.replace(self: str, old, new, count) -> str

使用str.replace的两种方法

  • 方法一:使用内置str的replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
  • 方法二:使用str变量的replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("from", "to")

完整演示

代码:

originStr = "Hello world"

# Use case 1: use builtin str's replace -> str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "world", "Crifan Li")
print("case 1: %s -> %s" % (originStr, replacedStr1))

# Use case 2: use str variable's replace -> strVariable.replace(old, new[, count])
replacedStr2 = originStr.replace("world", "Crifan Li")
print("case 2: %s -> %s" % (originStr, replacedStr2))

输出:

case 1: Hello world -> Hello Crifan Li
case 2: Hello world -> Hello Crifan Li

截图:

enter image description here

我的相关(中文)帖子:【详解】Python 3中字符串的替换str.replace

答案 6 :(得分:0)

您可以将str.replace()用作str.replace()链。假设您有一个像'Testing PRI/Sec (#434242332;PP:432:133423846,335)'这样的字符串,并且想用'#',':',';','/'替换所有的'-'符号。您可以用这种方式(正常方式)替换它,

>>> str = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> str = str.replace('#', '-')
>>> str = str.replace(':', '-')
>>> str = str.replace(';', '-')
>>> str = str.replace('/', '-')
>>> str
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

或以此方式(str.replace()的链)

>>> str = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> str
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'

答案 7 :(得分:0)

简单替换:.replace(旧,新,计数)。

text = "Apples taste Good."
print(text.replace('Apples', 'Bananas'))          # use .replace() on a variable
Bananas taste Good.          <---- Output

print("Have a Bad Day!".replace("Bad","Good"))    # Use .replace() on a string
Have a Good Day!             <----- Output

print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))  #Use many times
Dad is angry!                <----- Output

答案 8 :(得分:-2)

ss = s.replace(s.split()[1], +s.split()[1] + 'gy')
# should have no plus after the comma --i.e.,
ss = s.replace(s.split()[1], s.split()[1] + 'gy')