国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python replace()函數(shù)使用詳解,Python替換字符串

這篇具有很好參考價(jià)值的文章主要介紹了Python replace()函數(shù)使用詳解,Python替換字符串。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

「作者主頁(yè)」:士別三日wyx
「作者簡(jiǎn)介」:CSDN top100、阿里云博客專(zhuān)家、華為云享專(zhuān)家、網(wǎng)絡(luò)安全領(lǐng)域優(yōu)質(zhì)創(chuàng)作者
「推薦專(zhuān)欄」:小白零基礎(chǔ)《Python入門(mén)到精通》

replace() 可以「替換」字符串中的內(nèi)容

語(yǔ)法

string.replace( old, new, count )

參數(shù)

  • old :(必選,字符串類(lèi)型)被替換的字符串
  • new :(必選,字符串類(lèi)型)替換后的字符串
  • count :(可選,整型)替換的次數(shù)

返回值

  • 返回替換后的新字符串

實(shí)例:將字符串中的 “hello” 替換成 “world”

str1 = 'hello hello hello hello world'
str2 = str1.replace('hello', 'world')
print(str2)

輸出:

world world world world world

1、不改變?cè)址?/h2>

因?yàn)镻ython中的字符串是「不可變」的,所以 replace() 不會(huì)改變?cè)址膬?nèi)容,而是返回一個(gè)新的字符串。

我們分別打印替換前、后的兩個(gè)字符串「內(nèi)容」「內(nèi)存地址」。

str1 = 'hello hello hello hello world'
print(id(str1))
str2 = str1.replace('hello', 'world')
print(str1, id(str1))
print(str2, id(str2))

輸出:

2834751121168
hello hello hello hello world 2834751121168
world world world world world 2834751121568

可以看到,原字符串的內(nèi)容和內(nèi)存地址沒(méi)有發(fā)生變化。


2、指定替換次數(shù)

「不指定」次數(shù),默認(rèn)替換「所有」匹配到的字符串

str1 = 'hello_1 hello_2 hello_3 hello_4'
print(str1.replace('hello', 'world'))

輸出:

world_1 world_2 world_3 world_4

替換次數(shù)為「正數(shù)」時(shí),按照從左到右的順序替換,設(shè)置幾次就替換幾次

str1 = 'hello_1 hello_2 hello_3 hello_4'
print(str1.replace('hello', 'world', 1))
print(str1.replace('hello', 'world', 3))

輸出:

world_1 hello_2 hello_3 hello_4
world_1 world_2 world_3 hello_4

替換次數(shù)為「負(fù)數(shù)」時(shí),無(wú)論負(fù)幾,都會(huì)替換所有匹配到的內(nèi)容

str1 = 'hello_1 hello_2 hello_3 hello_4'
print(str1.replace('hello', 'world', -1))
print(str1.replace('hello', 'world', -3))

輸出:

world_1 world_2 world_3 world_4
world_1 world_2 world_3 world_4

指定的次數(shù)必須是「整型」,否則會(huì)報(bào)錯(cuò) TypeError: ‘str’ object cannot be interpreted as an integer

Python replace()函數(shù)使用詳解,Python替換字符串,Python,python,網(wǎng)絡(luò)安全,機(jī)器學(xué)習(xí),人工智能

或者 TypeError: integer argument expected,

Python replace()函數(shù)使用詳解,Python替換字符串,Python,python,網(wǎng)絡(luò)安全,機(jī)器學(xué)習(xí),人工智能


3、轉(zhuǎn)義符

字符串中的轉(zhuǎn)義符不會(huì)打印出來(lái),但 replace() 可以替換這些轉(zhuǎn)義符,比如替換換行符\n

str1 = 'hello world\n'
print(str1)
print(str1.replace('\n', ' new'))

輸出:

hello world

hello world new

從結(jié)果可以看到,替換前會(huì)換行,替換后不會(huì)換行,因?yàn)檗D(zhuǎn)義符被替換掉了。


4、替換列表、元組、字典的元素

對(duì)「列表」中的元素使用 replace() ,可以使用下面這種方式

arr = ['hello', 'hello', 'hello']
print([string.replace('hello', 'world') for string in arr])

輸出:

['world', 'world', 'world']

這種方式本質(zhì)上是生成了一個(gè)「新數(shù)組」,我們可以看一下內(nèi)存地址

arr = ['hello', 'hello', 'hello']
print(id(arr))
print(id([string.replace('hello', 'world') for string in arr]))

輸出:

1658941612416
1658941612544

或者使用「循環(huán)」的方式替換列表中的元素,這種方式不會(huì)生成新數(shù)組,替換前、后的內(nèi)存地址是一樣的。

arr1 = ['hello', 'hello', 'hello']
print(arr1, id(arr1))
for a in range(len(arr1)):
    arr1[a] = arr1[a].replace('hello', 'world')

print(arr1, id(arr1))

輸出:

['hello', 'hello', 'hello'] 1672076599552
['world', 'world', 'world'] 1672076599552

替換「元祖」中的元素,需要先轉(zhuǎn)成列表,再循環(huán)替換,替換完成再轉(zhuǎn)回元組,這種方式同樣會(huì)改變內(nèi)存地址。

tu = ('hello', 'hello', 'hello')
print(id(tu))
arr1 = list(tu)
for a in range(len(arr1)):
    arr1[a] = arr1[a].replace('hello', 'world')

tu = tuple(arr1)
print(tu, id(tu))

輸出:

2255689005696
('world', 'world', 'world') 2255689005824

替換「字典」的值,直接循環(huán)替換

dic = {'key1': 'zhangsan', 'key2': 'lisi'}

for a in dic:
    dic[a] = dic[a].replace('zhangsan', 'new')

print(dic)

輸出:

{'key1': 'new', 'key2': 'lisi'}

5、連續(xù)替換

因?yàn)?font color="blue"> replace() 返回的是一個(gè)字符串,所以我們可以對(duì)返回的結(jié)果再次replace(),比如下面這樣:

str1 = 'zhangsan lisi wangwu'
print(str1.replace('zhangsan', 'new').replace('lisi', 'new'))

輸出:

new new wangwu

有多個(gè)內(nèi)容需要替換時(shí),可以使用這種簡(jiǎn)化的方式。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-567158.html

到了這里,關(guān)于Python replace()函數(shù)使用詳解,Python替換字符串的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • C#用string.Replace方法批量替換某一類(lèi)字符串

    C#用string.Replace方法批量替換某一類(lèi)字符串

    目錄 一、關(guān)于字符串及其操作常識(shí) 二、String.Replace 方法 1.重載? 2.Replace(Char, Char) 3.Replace(String, String)? (1)實(shí)例: (2)生成結(jié)果: 4.Replace(String, String, StringComparison) 5.Replace(String, String, Boolean, CultureInfo) (1)實(shí)例 ????????在字符串操作中,可以使用字符串對(duì)象的 Split方法

    2024年02月02日
    瀏覽(42)
  • Python split()函數(shù)使用詳解,Python分割字符串

    Python split()函數(shù)使用詳解,Python分割字符串

    「作者主頁(yè)」: 士別三日wyx 「作者簡(jiǎn)介」: CSDN top100、阿里云博客專(zhuān)家、華為云享專(zhuān)家、網(wǎng)絡(luò)安全領(lǐng)域優(yōu)質(zhì)創(chuàng)作者 「推薦專(zhuān)欄」: 小白零基礎(chǔ)《Python入門(mén)到精通》 split() 可以按照指定 「分隔符」 對(duì)字符串進(jìn)行 「分割」 。 語(yǔ)法 參數(shù) str :(可選)指定分隔符,默認(rèn)為空字

    2024年02月12日
    瀏覽(22)
  • Python 按規(guī)則解析并替換字符串中的變量及函數(shù)

    1、按照一定規(guī)則解析字符串中的函數(shù)、變量表達(dá)式,并替換這些表達(dá)式。這些函數(shù)表達(dá)式可能包含其它函數(shù)表達(dá)式,即支持函數(shù)嵌套 2、函數(shù)表達(dá)式格式: ${ __函數(shù)名稱() }、${__函數(shù)名稱( 函數(shù)參數(shù) )} 3、變量表達(dá)式格式: ${ varName } 注意: 函數(shù)名稱以 __ 打頭 ${ 之間不能有空

    2024年02月05日
    瀏覽(26)
  • Python高頻面試題——如何在字符串中刪除指定字符,掌握strip()、replace()和re.sub ()正確使用方法!

    Python高頻面試題——如何在字符串中刪除指定字符,掌握strip()、replace()和re.sub ()正確使用方法!

    關(guān)于python刪除字符串是面試python測(cè)試開(kāi)發(fā)工程師的一個(gè)經(jīng)典問(wèn)題。問(wèn)題很簡(jiǎn)單,但是一下子就能測(cè)試出來(lái)被面試者是否能夠熟練的進(jìn)行python相關(guān)編碼工作! 對(duì)于有些臨時(shí)抱佛腳的同學(xué)來(lái)講,一看刪除,很自然就說(shuō)用remove 、del相關(guān)方法,聽(tīng)到這里,就知道面試者根本不知道這

    2024年02月08日
    瀏覽(30)
  • Python center()函數(shù)詳解,Python字符串居中

    Python center()函數(shù)詳解,Python字符串居中

    「作者主頁(yè)」: 士別三日wyx 「作者簡(jiǎn)介」: CSDN top100、阿里云博客專(zhuān)家、華為云享專(zhuān)家、網(wǎng)絡(luò)安全領(lǐng)域優(yōu)質(zhì)創(chuàng)作者 「推薦專(zhuān)欄」: 小白零基礎(chǔ)《Python入門(mén)到精通》 center() 可以使字符串 「居中」 ,并在兩邊 「填充」 指定數(shù)量的新字符。 語(yǔ)法 參數(shù) width :(必選,整型)字

    2024年02月13日
    瀏覽(64)
  • Python 中的字符串分割函數(shù) split() 詳解

    Python 中的字符串分割函數(shù) split() 詳解

    更多Python學(xué)習(xí)內(nèi)容:ipengtao.com 在 Python 編程中,處理字符串是一項(xiàng)常見(jiàn)的任務(wù)。字符串分割是其中的一個(gè)常見(jiàn)操作,而 Python 提供了強(qiáng)大的 split() 函數(shù),用于將字符串拆分成多個(gè)部分。本文將詳細(xì)介紹 split() 函數(shù)的用法、參數(shù)和示例代碼,以幫助大家充分利用這個(gè)功能。 spl

    2024年04月27日
    瀏覽(16)
  • 小程序原生使用wxs踩坑:使用正則replace字符串報(bào)錯(cuò)Unexpected token `/`

    小程序原生使用wxs踩坑:使用正則replace字符串報(bào)錯(cuò)Unexpected token `/`

    WXS(WeiXin Script)是小程序的一套腳本語(yǔ)言,結(jié)合 WXML,可以構(gòu)建出頁(yè)面的結(jié)構(gòu)。也就是說(shuō),使用wxs模塊可以在wxml中使用js方法,類(lèi)似于vue中的過(guò)濾器。 詳細(xì)介紹見(jiàn) wxs語(yǔ)法參考 在我新建的filter.wxs文件中寫(xiě)入方法后 然后就報(bào)錯(cuò): 然后我用 RegExp 對(duì)象轉(zhuǎn)了一下 編譯又報(bào)以下錯(cuò)誤

    2024年02月15日
    瀏覽(26)
  • ?【C語(yǔ)言】長(zhǎng)篇詳解,字符系列篇3-----strstr,strtok,strerror字符串函數(shù)的使用【圖文詳解?】

    ?【C語(yǔ)言】長(zhǎng)篇詳解,字符系列篇3-----strstr,strtok,strerror字符串函數(shù)的使用【圖文詳解?】

    歡迎來(lái)CILMY23的博客喔,本期系列為?【C語(yǔ)言】長(zhǎng)篇詳解,字符系列篇3-----strstr,strtok,strerror字符串函數(shù)的使用【圖文詳解?】,圖文講解各種字符串函數(shù),帶大家更深刻理解C語(yǔ)言中各種字符串函數(shù)的應(yīng)用,感謝觀看,支持的可以給個(gè)贊哇。? 前言 上一篇說(shuō)到,有不受長(zhǎng)度限

    2024年02月22日
    瀏覽(30)
  • Java中String字符串替換3種方法詳解

    replace() 方法用于將目標(biāo)字符串中的指定字符(串)替換成新的字符(串) 字符串.replace(String oldChar, String newChar) replaceFirst() 方法用于將目標(biāo)字符串中匹配某正則表達(dá)式的第一個(gè)子字符串替換成新的字符串 字符串.replaceFirst(String regex, String replacement) replaceAll() 方法用于將目標(biāo)字

    2024年02月11日
    瀏覽(22)
  • Python字符串替換的3種方法

    Python字符串替換筆記主要展示了如何在Python中替換字符串。Python中有以下幾種替換字符串的方法,本文主要介紹前三種。 replace方法(常用) translate方法 re.sub方法 字符串切片(根據(jù)Python字符串切片方法替換字符) Python replace方法把字符串中的old(舊字符串) 替換成new(新字符

    2024年02月05日
    瀏覽(27)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包