Fork me on GitHub

Python split() 方法

描述

Python split() 方法通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符,包括空格、换行(\n)、制表符(\t)等。

语法

split() 方法语法:

S.split([sep=None][,count=S.count(sep)])

参数

  • sep -- 可选参数,指定的分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • count -- 可选参数,分割次数,默认为分隔符在字符串中出现的总次数。

返回值

返回分割后的字符串列表。

实例

以下实例展示了 split() 方法的使用方法:

#!/usr/bin/python3

S = "this is string example....wow!!!"
print (S.split( ))
print (S.split('i',1))
print (S.split('w'))

以上实例输出结果如下:

['this', 'is', 'string', 'example....wow!!!']
['th', 's is string example....wow!!!']
['this is string example....', 'o', '!!!']
posted @ 2017-10-18 16:17  IT技术随笔  阅读(28940)  评论(1编辑  收藏  举报
返回顶部↑