新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonreplace()
python 中的replace()函數(shù)有助于在用“new”子字符串替換“old”子字符串后返回原始字符串的副本。該函數(shù)還允許指定舊字符串需要替換的次數(shù)。

夏河ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
**str.replace(old, new [, count]) ** #where old & new are strings
替換()參數(shù):
replace()函數(shù)接受三個參數(shù)。如果沒有給定 count 參數(shù),replace()方法將用新的子字符串替換所有舊的子字符串。replace()方法也可以與數(shù)字和符號一起使用。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 老的 | 要替換的舊子字符串 | 需要 |
| 新的 | 將替換舊子串的新子串 | 可選擇的 |
| 數(shù)數(shù) | 希望用新的子字符串替換舊的子字符串的次數(shù) | 可選擇的 |
替換()返回值
返回值始終是替換后的新字符串。此方法執(zhí)行區(qū)分大小寫的搜索。如果找不到指定的舊字符串,它將返回原始字符串。
| 投入 | 返回值 | | 線 | 字符串(舊的替換為新的) |
Python 中替換()方法的示例
示例 1:如何在 Python 中使用replace()?
string = 'Hii,Hii how are you'
# replacing 'Hii' with 'friends'
print(string.replace('Hii', 'friends'))
string = 'Hii, Hii how are you, Hii how are you, Hii'
# replacing only two occurences of 'Hii'
print(string.replace('Hii', "friends", 2))
輸出:
friends,friends how are you
Hii, friends how are you, friends how are you, Hii
示例 Python 中replace()的工作原理
string = 'Hii,Hii how are you'
# returns copy of the original string because of count zero
print(string.replace('Hii', 'friends',0))
string = 'Hii, Hii how are you, Hii how are you, Hii'
# returns copy of the original string because old string not found
print(string.replace('fine', "friends", 2))
輸出:
Hii,Hii how are you
Hii, Hii how are you, Hii how are you, Hii 當(dāng)前文章:創(chuàng)新互聯(lián)Python教程:Pythonreplace()
URL分享:http://fisionsoft.com.cn/article/djpsheo.html


咨詢
建站咨詢
