欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

語法

string.replace( old, new, count )

參數

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

返回值

  • 返回替換后的新字符串

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

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

輸出:

world world world world world

1、不改變原字符串

因為Python中的字符串是「不可變」的,所以 replace() 不會改變原字符串的內容,而是返回一個新的字符串。

我們分別打印替換前、后的兩個字符串「內容」和「內存地址」。

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

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

2、指定替換次數

「不指定」次數,默認替換「所有」匹配到的字符串

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

輸出:

world_1 world_2 world_3 world_4

替換次數為「正數」時,按照從左到右的順序替換,設置幾次就替換幾次

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

替換次數為「負數」時,無論負幾,都會替換所有匹配到的內容

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

指定的次數必須是「整型」,否則會報錯 TypeError: ‘str’ object cannot be interpreted as an integer

或者 TypeError: integer argument expected,

3、轉義符

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

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

輸出:

hello world
hello world new

從結果可以看到,替換前會換行,替換后不會換行,因為轉義符被替換掉了。

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

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

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

輸出:

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

這種方式本質上是生成了一個「新數組」,我們可以看一下內存地址

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

輸出:

1658941612416
1658941612544

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

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

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

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ù)替換

因為 replace() 返回的是一個字符串,所以我們可以對返回的結果再次replace(),比如下面這樣:

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

輸出:

new new wangwu

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

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

相關文章

  • appium+python自動化配置(adk、jdk、node.js)

    appium+python自動化配置(adk、jdk、node.js)

    這篇文章主要介紹了appium+python自動化配置(adk、jdk、node.js),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Pytest測試報告工具Allure用法介紹

    Pytest測試報告工具Allure用法介紹

    這篇文章介紹了Pytest測試報告工具Allure的用法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • Python3enumrate和range對比及示例詳解

    Python3enumrate和range對比及示例詳解

    這篇文章主要介紹了Python3enumrate和range對比及示例詳解,在Python中,enumrate和range都常用于for循環(huán)中,enumrate函數用于同時循環(huán)列表和元素,而range()函數可以生成數值范圍變化的列表,而能夠用于for循環(huán)即都是可迭代的,需要的朋友可以參考下
    2019-07-07
  • Python中datetime模塊參考手冊

    Python中datetime模塊參考手冊

    Python處理時間和日期方面的模塊,主要就是datetime、time、calendar三個模塊的使用。下面這篇文章主要給大家介紹的是Python中的datetime模塊,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • Python基于docker部署的Mysql備份查詢腳本

    Python基于docker部署的Mysql備份查詢腳本

    這篇文章主要來和大家分享Python基于docker部署的Mysql備份查詢的腳本,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起了解下
    2024-04-04
  • 詳解Python數據可視化編程 - 詞云生成并保存(jieba+WordCloud)

    詳解Python數據可視化編程 - 詞云生成并保存(jieba+WordCloud)

    這篇文章主要介紹了Python數據可視化編程 - 詞云生成并保存(jieba+WordCloud),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • 正則表達式在Python中的應用小結

    正則表達式在Python中的應用小結

    正則表達式是一種強大的文本模式匹配工具,它可以幫助我們快速地檢索、替換或提取字符串中的特定模式,在本文中,我將通過一些示例代碼,詳細介紹正則表達式在Python中的應用,感興趣的朋友一起看看吧
    2024-07-07
  • 使用Python內置模塊與函數進行不同進制的數的轉換

    使用Python內置模塊與函數進行不同進制的數的轉換

    這篇文章主要介紹了使用Python內置模塊與函數進行不同進制的數的轉換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Python qqbot 實現qq機器人的示例代碼

    Python qqbot 實現qq機器人的示例代碼

    這篇文章主要介紹了Python qqbot 實現qq機器人的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • Python自動發(fā)郵件腳本

    Python自動發(fā)郵件腳本

    本文主要介紹了Python自動發(fā)郵件腳本的相關知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03

最新評論