python .split
时间: 2023-10-19 11:32:23 浏览: 142
Python中的split()函数用于拆分字符串。它通过指定分隔符对字符串进行切片,并返回分割后的字符串列表。如果不指定分隔符,则默认以空格、换行、制表符为分隔符。如果指定了分隔次数,则只分隔指定次数的子字符串。
下面是几个示例:
1. 使用空格作为分隔符:`a = "I love China",a.split()`,输出结果为 `['I', 'love', 'China']`
2. 使用逗号和空格作为分隔符:`b = "I love China, and you, you",b.split(", ")`,输出结果为 `['I love China', 'and you', 'you']`
3. 使用井号(#)作为分隔符:`c = "I#love#China#andyou#you",c.split("#")`,输出结果为 `['I', 'love', 'China', 'andyou', 'you']`
4. 指定分隔次数为1:`d = "I#love#China#andyou#you",d.split("#", 1)`,输出结果为 `['I', 'love#China#andyou#you']`
5. 指定分隔次数为15:`e = "with great power comes great responsibility. I love China and you you",e.split(" ", 15)`,输出结果为 `['with', 'great', 'power', 'comes', 'great', 'responsibility.', 'I', 'love', 'China', 'and', 'you', 'you']`
总之,split()函数可以根据指定的分隔符将字符串拆分成多个子字符串,并以列表的形式返回。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python中超好用的split()函数,详解](https://blog.csdn.net/m0_57468722/article/details/128564266)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python中split()函数的用法详解](https://blog.csdn.net/qq_44985415/article/details/128701783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文