掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
python 中的split()函數(shù)通過使用指定的分隔符拆分原始字符串來幫助返回字符串列表。

成都創(chuàng)新互聯(lián)公司長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為單縣企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、成都網(wǎng)站制作,單縣網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
**str.split([separator [, maxsplit]])** #where separator may be a character,symbol,or space
split()函數(shù)接受兩個參數(shù)。如果沒有給定分隔符參數(shù),它將接受任何空白(空格、換行符等)。)作為分隔符。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 分離器 | 它是一個分隔符。字符串在指定的分隔符處拆分。 | 可選擇的 |
| maxsplit | maxsplit 定義了拆分的最大數(shù)量。 | |
| max split 的默認值為-1。 | 可選擇的 |
返回值是字符串列表。如果拆分計數(shù)是 maxsplit,那么我們在輸出列表中有 maxsplit+1 個項目。如果找不到指定的分隔符,則將整個字符串作為元素的列表作為輸出返回。
| 投入 | 返回值 | | 線 | 字符串列表 |
split()方法的示例split()方法在 Python 中是如何工作的? string= 'Hii How are you'
# splits at space
print(string.split())
fruits= 'apple, orange, grapes'
# splits at ','
print(fruits.split(', '))
# Splits at ':' but not exist so return original
print(fruits.split(':'))
輸出:
['Hii', 'How', 'are', 'you']
['apple', 'orange', 'grapes']
['apple, orange, grapes']split()的工作方式? 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'] 
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流