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

python中的字符串切割 maxsplit

 更新時(shí)間:2022年12月20日 08:47:03   作者:LanLanDeMing  
這篇文章主要介紹了python中的字符串切割 maxsplit,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python 字符串切割 maxsplit

my_str.split(str1, maxsplit)

str1 可以不寫(xiě),默認(rèn)是空白字符(" " “\t” “\n”)

將my_str 這個(gè)字符串按照str1 進(jìn)行切割, maxsplit 割幾次

my_str = "hello world itcast and itcastcpp"
my_str1 = my_str.split(" ")
print(my_str1)

my_str2 = my_str.split(" ", 1)
print(my_str2)

my_str3 = my_str.split()  # 用的最多
print(my_str3)

my_str4 = my_str.split("itcast")
print(my_str4)


# 輸出結(jié)果是
['hello', 'world', 'itcast', 'and', 'itcastcpp']
['hello', 'world itcast and itcastcpp']
['hello', 'world', 'itcast', 'and', 'itcastcpp']
['hello world ', ' and ', 'cpp']

python字符串切割split和rsplit函數(shù)

1. split(sep, maxsplit)

切分字符串,返回切分后的列表

sep,分隔符,默認(rèn)空格

maxsplit,切分次數(shù),默認(rèn)最大次數(shù),從起始位置開(kāi)始計(jì)數(shù)

示例1:默認(rèn)

s = 'a b c'
res = s.split()
res
['a', 'b', 'c']

示例2:指定參數(shù)

s = 'a b c'
res = s.split(sep=' ', maxsplit=1)
res
['a', 'b c']

示例3:位置參數(shù)

s = 'a.b.c'
res = s.split('.', 1)
res
['a', 'b.c']

2. rsplit(sep, maxsplit)

類似split,區(qū)別為從結(jié)尾位置開(kāi)始計(jì)數(shù)

sep,分隔符,默認(rèn)空格

maxsplit,切分次數(shù),默認(rèn)最大次數(shù),從起始結(jié)尾開(kāi)始計(jì)數(shù)

示例1:默認(rèn)

s = 'a b c'
res = s.rsplit()
res
['a', 'b', 'c']

示例2:指定參數(shù)

s = 'a b c'
res = s.rsplit(sep=' ', maxsplit=1)
res
['a b', 'c']

示例3:位置參數(shù)

s = 'a.b.c'
res = s.rsplit('.', 1)
res
['a.b', 'c']

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論