新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python rsplit()
python 中的rsplit()函數(shù)通過拆分原始字符串來幫助返回字符串列表。這里的拆分是從右端開始,并使用指定的分隔符。

**str.rsplit([separator [, maxsplit]])** #where separator may be a character,symbol,or space
rsplit()參數(shù):
rsplit()函數(shù)接受兩個(gè)參數(shù)。如果沒有給定分隔符參數(shù),它將接受任何空白(空格、換行符等)。)作為分隔符。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 分離器 | 它是一個(gè)分隔符。字符串在指定的分隔符處拆分。 | 可選擇的 |
| maxsplit | maxsplit 定義了拆分的最大數(shù)量。 | |
| max split 的默認(rèn)值為-1。 | 可選擇的 |
返回值
返回值是字符串列表。如果拆分計(jì)數(shù)是 maxsplit,那么我們?cè)谳敵隽斜碇杏?maxsplit+1 個(gè)項(xiàng)目。如果找不到指定的分隔符,則將整個(gè)字符串作為元素的列表作為輸出返回。
| 投入 | 返回值 | | 線 | 字符串列表 |
Python 中rsplit()方法的示例
示例rsplit()方法在 Python 中是如何工作的?
string= 'Hii How are you'
# splits at space
print(string.rsplit())
fruits= 'apple, orange, grapes'
# splits at ','
print(fruits.rsplit(', '))
# Splits at ':' but not exist so return original
print(fruits.rsplit(':'))
輸出:
['Hii', 'How', 'are', 'you']
['apple', 'orange', 'grapes']
['apple, orange, grapes']示例 2:當(dāng)指定 maxsplit 時(shí)rsplit()的工作方式?
fruits= 'apple, orange, grapes, strawberry'
# maxsplit: 2
print(fruits.split(', ', 2))
# maxsplit: 1
print(fruits.split(', ', 1))
# maxsplit: 5
print(fruits.split(', ', 5))
# maxsplit: 0
print(fruits.split(', ', 0))
輸出:
['apple, orange', 'grapes', 'strawberry']
['apple, orange, grapes', 'strawberry']
['apple', 'orange', 'grapes', 'strawberry']
['apple, orange, grapes, strawberry'] 當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:Python rsplit()
本文地址:http://m.5511xx.com/article/dhpdghg.html


咨詢
建站咨詢
