python3 字串方法

2022-09-20 03:09:10 字數 949 閱讀 2123

1 join方法

test = 「nancy」

v = "*".join(test)

print(v)

執行結果:n*a*n*c*y

2 split方法

不帶引數時,字串以空格、\n、、\t進行分割

例子:          

test1 = "nancy alex tim\t james\nfigo"

v1 = test1.split()

print(v1)

執行結果:['nancy', 'alex', 'tim', 'james', 'figo']

2.帶引數時,以指定的字串對原字串進行分割

例子:

test = "jiosnfsdnngkldsfngjaj"

v = test.split("s")

print(v)

執行結果:['jio', 'nf', 'dnngkld', 'fngjaj']

理解:指定的字串作為分割引數,分割原字串時,從左至右依次進行,遇到指定的字串後將左邊的所有字元作為列表的第乙個元素,

指定字串丟棄,繼續進行分割,下一次遇到指定的分割字串後,將其前面的所有字串作為列表的第二個元素,指定字串丟

棄,依次查詢,直到需分割的字串結尾。

3.find方法

的位置可選,預設為全部字串

test = "jiosnfsdnngkldsfngjaj"

v = test.find("fng")

print(v)

執行結果:15

test = "jiosnfsdnngkldsfngjaj"

v = test.find("xa")

print(v)

執行結果:-1

python3字串方法總結

1 capitalize 將字串的第乙個字元轉換為大寫 2center width,fillchar 返回乙個指定的寬度 width 居中的字串,fillchar 為填充的字元,預設為空格。3count str,beg 0,end len string 返回 str 在 string 裡面出現的次數...

Python 3 字串 rsplit 方法

str.rsplit sep none,maxsplit 1 a 我 愛 yi 條 chai a.rsplit 與 split 無異 我 愛 yi 條 chai b wo 愛 u b.rsplit wo 愛 u c 我 愛 一 條 柴 c.rsplit 我 愛 一 條 柴 d d.rsplit e ...

Python 3 字串 split 方法

str.split sep none,maxsplit 1 a 我 愛 yi 條 chai a.split 我 愛 yi 條 chai b wo 愛 u b.split wo 愛 u c 我 愛 一 條 柴 c.split 我 愛 一 條 柴 d d.split e e.split f f.spli...