如何在 Python 3.x 中使用 string.replace()
- 2024-12-24 08:55:00
- admin 原创
- 121
问题描述:
在Python string.replace()
3.x 上已弃用。 有什么新方法可以做到这一点?
解决方案 1:
与 2.x 一样,使用str.replace()
。
例子:
>>> 'Hello world'.replace('world', 'Guido')
'Hello Guido'
解决方案 2:
replace()
是python3中的一个方法<class 'str'>
:
>>> 'hello, world'.replace(',', ':')
'hello: world'
解决方案 3:
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
解决方案 4:
您可以将str.replace()用作str.replace()链。假设您有一个字符串'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
,并且想要将所有'#',':',';','/'
符号替换为'-'
。您可以通过以下方式(正常方式)进行替换,
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'
>>> string = string.replace('#', '-')
>>> string = string.replace(':', '-')
>>> string = string.replace(';', '-')
>>> string = string.replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
或者这种方式( str.replace()链)
>>> string = 'Testing PRI/Sec (#434242332;PP:432:133423846,335)'.replace('#', '-').replace(':', '-').replace(';', '-').replace('/', '-')
>>> string
'Testing PRI-Sec (-434242332-PP-432-133423846,335)'
解决方案 5:
str.replace
官方文档Python 3
官方文档:Python 3str.replace
str.replace(旧的,新的[,计数])
返回字符串的副本,其中所有出现的子字符串 old 均被 new 替换。如果给出了可选参数 count,则仅替换前 count 个出现的位置。
对应VSCode
的语法说明为:
str.replace(self:str,旧的,新的,计数) -> str
两种使用方法str.replace
方法 1:使用内置 str 的 replace ->
str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
方法2:使用str变量的替换->
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
截屏:
我的相关(中文)帖子:【详解】Python 3中字符串的替换str.replace
解决方案 6:
尝试一下:
mystring = "This Is A String"
print(mystring.replace("String","Text"))
解决方案 7:
为了提高可读性:将一些字符附加到位置固定的单词
例如:通过添加后缀-ly将形容词转换为副词。
为了提高可读性,您可以将后缀放在行尾以提高可读性。为此,请在split()
inside使用replace()
:
s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'
解决方案 8:
简单替换: .replace(old, new, count) 。
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
解决方案 9:
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')