Python split()方法

Python 教程 · 2019-02-02 17:41:40

描述

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

语法

split() 方法语法:

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

参数

  • str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • num -- 分割次数。默认为 -1, 即分隔所有。

返回值

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

实例

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

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ); # 以空格为分隔符,包含 \n print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个

以上实例输出结果如下:

['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

以下实例以 # 号为分隔符,指定第二个参数为 1,返回两个参数列表。

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- txt = "Google#Codercto#Taobao#Facebook" # 第二个参数为 1,返回两个参数列表 x = txt.split("#", 1) print x

以上实例输出结果如下:

['Google', 'Codercto#Taobao#Facebook']

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Getting Started with C++ Audio Programming for Game Development

Getting Started with C++ Audio Programming for Game Development

David Gouveia

Written specifically to help C++ developers add audio to their games from scratch, this book gives a clear introduction to the concepts and practical application of audio programming using the FMOD li......一起来看看 《Getting Started with C++ Audio Programming for Game Development》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具