Python 字元的替換

2021-10-19 05:11:10 字數 3719 閱讀 8614

python 字串轉列表,列表轉字串,字串替換

突然想把python基礎再鞏固鞏固,所以寫部落格來記錄一下,**不對,歡迎指正,下為正文。

關於字串: 字串是不可變序列,拆分字串是在拷貝的字串上進行的,並不會改變原有序列

拆分字串

split方法:此方法可以接收兩個引數,第乙個引數是分隔符,預設是所有的空字元,包括 空格,換行 製表符等,拆分過程中會消耗分隔符,所以,拆分結果不包含分隔符。第二個引數 是乙個數字,預設預設 預設時全分割,也可以用 maxsplit 來指定分割次數。

例如:定義字串:

s = 『hello world』

l = 「」「hi there , my name is blankdog

do you like me 「」」

#不傳引數時,預設分隔符為所有空字元

split_nopam = s.split()

print(split_nopam)

#乙個空格

split_pam = s.split(』 『)

print(split_pam)

#兩個空格

split_pam1 = s.split(』 ')

print(split_pam1)

#使用world為分隔符,會看到,split會消耗掉分隔符,而不顯示

split_pam2 = s.split(『world』)

print(split_pam2)

#空字元包含,空格,,多個空格,換行符等

l_split = l.split()

print(l_split)

split方法的第二個引數,指定分割次數,直接在第乙個引數後面加乙個數字(按位置傳參),或者使用 maxsplit=num 來指定

#根據空格分割三段(按位置傳參)

l_split_1 = l.split(』 『,3)

print(l_split_1)

#根據空格分割三段(按位置傳參)

l_split_1 = l.split(』 ',3)

print(l_split_1)

#指定引數

l_split_2 = l.split(maxsplit=3)

print(l_split_2)

#錯誤用法,不按位置傳參,直接傳遞分割幾次數字引數

l.split(3)

split()方法是從左往右遍歷,與之相對的是,rsplit()方法,從右往左便利,比較少用,會有奇效

l_split_3 = l.rsplit(maxsplit=2)

print(l_split_3)

拆分字串還有一種方法, splitlines() 這個方法按照行來拆分字串(預設分隔符 按照行(』\r』, 『\r\n』, \n』)分隔),接受乙個引數,true,或者 false,分別決定換行符是否被保留,預設為false,即不保留換行符

複製**

複製**

#保留換行符

s1 = 「」「ab c

d e f

gh「」」

splitlines_true = s1.splitlines(true)

print(splitlines_true)

複製**

複製**

複製**

複製**

#不保留換行符

s2 = 「」「ab c

d e f

gh「」」

splitlines_true = s2.splitlines(false)

print(splitlines_true)

複製**

複製**

列表轉字串

join() 方法可以將列表鏈結為字串
s_list = [『hi』,『my』,『name』,『is』,『blank』,『dog』]

#鏈結為以空格為分隔符 的字串

print( 』 '.join(s_list) )

#鏈結為以下劃線為分隔符的字串

print( 『_』.join(s_list) )

2. 替換字串 替換字串的場景如下:大小寫替換,特定字元轉換,自定義片段替換…

複製**

複製**

#首個字母替換為大寫

rep_s = 『hello world』

rep_res = rep_s.capitalize()

print(rep_res)

#所有單詞首字母變為大寫

rep_res1 = rep_s.title()

print(rep_res1)

#所有小寫字母轉換為大寫

rep_res2 = rep_s.upper()

print(rep_res2)

#所有大寫寫字母轉換為小寫

rep_s1 = 『hello world』

rep_res3 = rep_s1.lower()

print(rep_res3)

#所有大寫替換為小寫,小寫替換為大寫

rep_s2 = 『hello world』

rep_res4 = rep_s2.swapcase()

print(rep_res4)

#所有tab建轉換為空格,tab建預設空格數是8

rep_s3 = 「hello world」

rep_res5 = rep_s3.expandtabs(tabsize=4)

print(rep_s3)

複製**

複製**

#替換字串

rep_s4 = 『hi world』

rep_res6 = rep_s4.replace(『hi』,『hello』)

print(rep_res6)

#替換字串,可以指定替換次數

rep_s5 = 『hi world hi world hi world』

rep_res7 = rep_s5.replace(『hi』,『hello』,2)

print(rep_res7)

複製**

複製**

#字串開頭空格,或者指定字元替換為空

rep_s6 = 』 hello world』

rep_res8 = rep_s6.lstrip(『h』)

print(rep_res8)

#指定字元為空

rep_s6 = 『hello world』

rep_res8 = rep_s6.lstrip(『h』)

print(rep_res8)

#指定字元為空

rep_s6 = 『hello world』

rep_res8 = rep_s6.lstrip(『h』)

print(rep_res8)

#字串末尾空格,或者指定字元替換為空

rep_s7 = 'hello world 』

rep_res9 = rep_s7.lstrip()

print(rep_res9)

#字串首位空格替換,或者指定字元替換

rep_s8 = 』 hello world 』

rep_res10 = rep_s8.strip()

print(rep_res10)

#刪除首末位置的指定字串

rep_s9 = 『*hello world

』rep_res11 = rep_s9.strip(』』)

print(rep_res11)

以上,為所學習關於字串處理的基礎,其他幾種方式後續再來補充。

python替換指定字元

有如下一段文字,要求將中括號後的第乙個分號 替換為換行符 souma,kousaku kanda,fumie masuko,takayoshi tokyo univ agr,fac bioind,abashiri,hokkaido 0992493,japan wang,peng jgfdsa uni...

python字串替換

print replace實現多個子串替換 strr abaabbaccaaeeaa print 原字串 n format strr e strr.replace aa str 0 print 替換後的字串 n format e print split實現字串替換 strr abaabbaccaae...

python字元對映表和字元替換

python中有乙個內建函式maketrans 可以對兩個字串進行字元對映,建立出對映表。結構如下 str maketrans intab,outtab 當使用該函式時,將會把intab中的字串對out字串中的字元進行一一對應。而使用translate 函式則可以利用對映表字元對指定字串的字元進行替...