簡體   English   中英

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

[英]How to use string.replace() in python 3.x

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

在 2.x 中,使用str.replace()

例子:

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

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 -> 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

截屏:

在此處輸入圖片說明

我的相關(中文)帖子: 【詳解】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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM