[英]How to use string.replace() in python 3.x
string.replace()
在 python 3.x 上已棄用。 這樣做的新方法是什么?
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
您可以使用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)'
試試這個:
mystring = "This Is A String"
print(mystring.replace("String","Text"))
僅供參考,當將一些字符附加到字符串內的任意位置固定詞時(例如,通過添加后綴-ly將形容詞更改為副詞),您可以將后綴放在行尾以提高可讀性。 為此,請在replace()
使用split()
replace()
:
s="The dog is large small"
ss=s.replace(s.split()[3],s.split()[3]+'ly')
ss
'The dog is largely small'
Python 3
str.replace
的官方文檔官方文檔: Python 3 的str.replace
str.replace(舊的,新的[,計數])
返回字符串的副本,其中所有出現的子字符串 old 都被 new 替換。 如果給出了可選參數計數,則僅替換第一個計數出現。
對應的VSCode
的語法說明是:
str.replace(self: str, old, new, count) -> str
str.replace
兩種方法str.replace(strVariable, old, new[, count])
replacedStr1 = str.replace(originStr, "from", "to")
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
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')
簡單替換: .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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.