View Post


python中的split方法

参考:http://www.runoob.com/python/att-string-split.html

Python split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

语法:

str.split(str="", num=string.count(str))

其中str — 分隔符,默认所有的空字符串,包括空格、换行(\n)、制表符(\t)等。

       num — 分割次数

返回字符串列表。

实例代码

if __name__ == \'__main__\':
    s = "IndexError: string index out of range"
    print(s.split(\'i\'))
    print(s.split(\'i\',1))

输出为:
"""
[\'IndexError: str\', \'ng \', \'ndex out of range\']
[\'IndexError: str\', \'ng index out of range\']
"""

  

版权声明:本文为xiaodongsuibi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xiaodongsuibi/p/8655285.html