最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2023年07月26日 10:39:52   作者:士別三日wyx  
Python中的replace()方法是把字符串中的old(舊字符串)替換成new(新字符串),如果指定第三個參數(shù)max,則替換次數(shù)不超過max次(將舊的字符串用心的字符串替換不超過max次,本文就給大家講講Python replace()函數(shù)的使用方法,需要的朋友可以參考下

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

語法

string.replace( old, new, count )

參數(shù)

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

返回值

  • 返回替換后的新字符串

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

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

輸出:

world world world world world

1、不改變原字符串

因?yàn)镻ython中的字符串是「不可變」的,所以 replace() 不會改變原字符串的內(nèi)容,而是返回一個新的字符串。

我們分別打印替換前、后的兩個字符串「內(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)存地址沒有發(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è)置幾次就替換幾次

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ù)」時,無論負(fù)幾,都會替換所有匹配到的內(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ù)必須是「整型」,否則會報錯 TypeError: ‘str’ object cannot be interpreted as an integer

或者 TypeError: integer argument expected,

3、轉(zhuǎn)義符

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

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

輸出:

hello world
hello world new

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

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

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

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

輸出:

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

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

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

輸出:

1658941612416
1658941612544

或者使用「循環(huán)」的方式替換列表中的元素,這種方式不會生成新數(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)回元組,這種方式同樣會改變內(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)?nbsp;replace() 返回的是一個字符串,所以我們可以對返回的結(jié)果再次replace(),比如下面這樣:

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

輸出:

new new wangwu

有多個內(nèi)容需要替換時,可以使用這種簡化的方式。

到此這篇關(guān)于Python替換字符串replace()函數(shù)使用方法詳解的文章就介紹到這了,更多相關(guān)Python replace()函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法

    Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法

    今天小編就為大家分享一篇Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 用python寫個顏值評分器篩選最美主播

    用python寫個顏值評分器篩選最美主播

    這篇文章主要介紹了我如何用python寫顏值評分器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • 詳解Python中魔法方法的使用

    詳解Python中魔法方法的使用

    Python的魔法方法,也稱為dunder(雙下劃線)方法,是可以讓你對類添加“魔法”的特殊方法。本文主要來和大家聊聊魔法方法的使用,需要的可以參考一下
    2022-12-12
  • Python搭建NLP模型的步驟和代碼詳解

    Python搭建NLP模型的步驟和代碼詳解

    這篇文章主要為大家詳細(xì)介紹了Python搭建NLP模型的詳細(xì)步驟,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • Python實(shí)現(xiàn)API開發(fā)的詳細(xì)教程

    Python實(shí)現(xiàn)API開發(fā)的詳細(xì)教程

    在現(xiàn)代軟件開發(fā)中,API扮演著至關(guān)重要的角色,API接口用于不同軟件組件之間的通信和數(shù)據(jù)交換,實(shí)現(xiàn)了系統(tǒng)之間的互操作性,Python作為一種簡單易用且功能強(qiáng)大的編程語言,廣泛應(yīng)用于API接口的開發(fā),本文將詳細(xì)介紹如何使用Python開發(fā)API接口,需要的朋友可以參考下
    2024-12-12
  • python實(shí)現(xiàn)模擬按鍵,自動翻頁看u17漫畫

    python實(shí)現(xiàn)模擬按鍵,自動翻頁看u17漫畫

    這篇文章主要介紹了python實(shí)現(xiàn)模擬按鍵,自動翻頁看u17漫畫,十分簡單實(shí)用,需要的朋友可以參考下
    2015-03-03
  • TensorFlow索引與切片的實(shí)現(xiàn)方法

    TensorFlow索引與切片的實(shí)現(xiàn)方法

    這篇文章主要介紹了TensorFlow索引與切片的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • python中文編碼問題小結(jié)

    python中文編碼問題小結(jié)

    這篇文章主要介紹了python中文編碼問題,是Python程序設(shè)計中比較常見的一類問題,本文以實(shí)例形式對此進(jìn)行了較為詳細(xì)的總結(jié),需要的朋友可以參考下
    2014-09-09
  • Python自動化管理文件的6個實(shí)用腳本總結(jié)

    Python自動化管理文件的6個實(shí)用腳本總結(jié)

    在日常工作中,手動管理文件既耗時又容易出錯,而Python提供了強(qiáng)大的工具來自動完成這些任務(wù),本文為大家整理了6個Python自動化管理文件的實(shí)用腳本,希望對大家有所幫助
    2026-03-03
  • python flask框架實(shí)現(xiàn)重定向功能示例

    python flask框架實(shí)現(xiàn)重定向功能示例

    這篇文章主要介紹了python flask框架實(shí)現(xiàn)重定向功能,結(jié)合實(shí)例形式分析了flask框架重定向功能的實(shí)現(xiàn)與使用方法,需要的朋友可以參考下
    2019-07-07

最新評論

招远市| 东港市| 吉木萨尔县| 临湘市| 报价| 巨鹿县| 本溪市| 玛沁县| 聊城市| 平原县| 麻阳| 新晃| 太原市| 保德县| 高州市| 长顺县| 当阳市| 望都县| 柏乡县| 云霄县| 游戏| 和林格尔县| 屯留县| 民权县| 修武县| 枣庄市| 涟水县| 山东省| 大关县| 察隅县| 巫山县| 廉江市| 子长县| 泗洪县| 调兵山市| 郓城县| 大名县| 秦皇岛市| 周口市| 颍上县| 东莞市|