string.replace()在python 3.x上已弃用。新的方法是什么?


当前回答

Replace()是python3中<class 'str'>的一个方法:

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

其他回答

Replace()是python3中<class 'str'>的一个方法:

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

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

如2。X,使用str.replace()。

例子:

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

试试这个:

mystring = "This Is A String"
print(mystring.replace("String","Text"))
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')