当前位置:  首页>> 技术小册>> Python合辑2-字符串常用方法

9、split()

对字符串做分隔处理,最终的结果是一个列表。

  1. s = 'string methods in python'.split()
  2. print(s)
  3. # ['string', 'methods', 'in', 'python']

当不指定分隔符时,默认按空格分隔。

  1. s = 'string methods in python'.split(',')
  2. print(s)
  3. # ['string methods in python']

此外,还可以指定字符串的分隔次数。

  1. s = 'string methods in python'.split(' ', maxsplit=1)
  2. print(s)
  3. # ['string', 'methods in python']

该分类下的相关小册推荐: