日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python中的字符串切割?maxsplit_python

作者:LanLanDeMing ? 更新時間: 2023-01-19 編程語言

python 字符串切割 maxsplit

my_str.split(str1, maxsplit)

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

將my_str 這個字符串按照str1 進行切割, 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)


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

python字符串切割split和rsplit函數

1. split(sep, maxsplit)

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

sep,分隔符,默認空格

maxsplit,切分次數,默認最大次數,從起始位置開始計數

示例1:默認

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

示例2:指定參數

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

示例3:位置參數

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

2. rsplit(sep, maxsplit)

類似split,區別為從結尾位置開始計數

sep,分隔符,默認空格

maxsplit,切分次數,默認最大次數,從起始結尾開始計數

示例1:默認

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

示例2:指定參數

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

示例3:位置參數

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

總結

原文鏈接:https://blog.csdn.net/LanlanDeming/article/details/103318399

欄目分類
最近更新