Python 分割字串與拼接字串

2021-08-15 12:29:47 字數 827 閱讀 5487

+ 號:實現字串連線

>>> "zhrq" + "95"

'zhrq95'

>>> a = 95

>>> b = "zhrq"

>>> print b + `a` # 注:是反引號,因為其容易看錯,此方法不推薦

zhrq95

>>> print b + str(a)

zhrq95

>>> print b + repr(a) #repr(a)與上面的類似

zhrq95

split:這個函式的作用是將字串根據某個分割符進行分割

>>> a = 「i love python」

>>> a.split(」 「) # 這是用空格作為分割,得到了乙個名字叫做列表(list)的返回值

['i',  'love',  'python']

>>> b = "www.csdn.net"

>>> b.split(「.」)

['www',  'csdn',  'net']

join:拼接字串

用「+」能夠拼接字串,但不是什麼情況下都能夠如願的。比如,將列表中的每個字元(串)元素拼接成乙個字串,並且用某個符號連線,如果用「+」,就比較麻煩了,用字串的 join 就比較容易實現。

>>> b

'www.csdn.net'

>>> c = b.split(「.」)

>>> c

['www',  'csdn',  'net']

>>> 「.」.join(c)

'www.csdn.net'

>>> 「*」.join(c)

'www*csdn*net'

python分割和拼接字串

python分割和拼接字串的例項,使用了string的split和join 方法,並對這二個方法做說明。關於string的split 和 join 方法 對匯入os模組進行os.path.splie os.path.join 貌似是處理機制不一樣,但是功能上一樣。1.string.split str...

python分割和拼接字串

關於string的split 和 join 方法 對匯入os模組進行os.path.splie os.path.join 貌似是處理機制不一樣,但是功能上一樣。1.stri程式設計客棧ng.split str num string.count str 以str為分隔,符切片string,如果num有...

字串的拼接分割

string s1 s1 string s2 s2 string s3 s1 s2 string s4 wahaha cout 分割字串 c 中string類並沒有提供split函式分割.參考他人c split 函式的實現 如果是切分檔名 c windows aaa.txt 獲得aaa.txt 通過...