Python split()的用法以及如何利用空格进行输入
2018-02-23 17:04
666 查看
Python 3.6.4 Document 中关于 str.split() 原内容如下:str.split(sep=None, maxsplit=-1)¶
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is notspecified or -1, then there is no limit on the number of splits(all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and aredeemed to delimit empty strings (for example, '1,,2'.split(',') returns['1', '', '2']). The sep argument may consist of multiple characters(for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']).Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm isapplied: runs of consecutive whitespace are regarded as a single separator,and the result will contain no empty strings at the start or end if thestring has leading or trailing whitespace. Consequently, splitting an emptystring or a string consisting of just whitespace with a None separatorreturns [].Python Document原文str.split(sep=None,maxsplit=-1)maxsplit为分隔数(间隔的数量)
如果maxsplit为-1或者没有指定数值那么会按照所有相应的分隔符(sep)进行分隔。如果maxsplit指定了数值,则按照从左到右maxsplit个分隔符(sep)进行分隔。sep为分隔符
如果指定了sep的值(如sep=',' )则按照sep的值进行分隔。如果没有指定sep的值或者sep=None,那么默认空格为分隔符且执行完成后字符串开头和结尾不会存在空格。例如:
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is notspecified or -1, then there is no limit on the number of splits(all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and aredeemed to delimit empty strings (for example, '1,,2'.split(',') returns['1', '', '2']). The sep argument may consist of multiple characters(for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']).Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm isapplied: runs of consecutive whitespace are regarded as a single separator,and the result will contain no empty strings at the start or end if thestring has leading or trailing whitespace. Consequently, splitting an emptystring or a string consisting of just whitespace with a None separatorreturns [].Python Document原文str.split(sep=None,maxsplit=-1)maxsplit为分隔数(间隔的数量)
如果maxsplit为-1或者没有指定数值那么会按照所有相应的分隔符(sep)进行分隔。如果maxsplit指定了数值,则按照从左到右maxsplit个分隔符(sep)进行分隔。sep为分隔符
如果指定了sep的值(如sep=',' )则按照sep的值进行分隔。如果没有指定sep的值或者sep=None,那么默认空格为分隔符且执行完成后字符串开头和结尾不会存在空格。例如:
>>> '1,2,3'.split(',') ['1', '2', '3'] >>> '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] >>> '1,2,,3,'.split(',') ['1', '2', '', '3', ''] >>> '1 2 3'.split() ['1', '2', '3'] >>> '1 2 3'.split(maxsplit=1) ['1', '2 3'] >>> ' 1 2 3 '.split() ['1', '2', '3']如果想利用空格输入数字也可以用split()达到目的例如:
num =[int(x) for x in input().split()] print(num) input:3 2 8 5 output:[3, 2, 8, 5]最终得到一个由int组成的列表
相关文章推荐
- 如何利用Python对中文进行分词处理
- python 3-4 如何进行反向迭代以及如何实现反向迭代 __reversed__ yield
- python3 下的文件输入输出特性以及如何覆盖文件内容和接下去输入
- Python 的切片操作以及 利用步长对序列进行倒序取值
- 利用函数统计输入字符中字母、空格、数字和其他字符个数(Python实现)
- 利用Python-caffe进行图像分类,卷积核的显示,激活值特征图的显示以及全连接层直方图显示
- 如何利用Python和win32编程避免重复性体力劳动(二)——菜单操作:GetMenu,GetSubMenu,GetMenuItemID以及wParam的HIWORD&LOWORD
- Python利用公共键如何对字典列表进行排序详解
- VScode&Python:如何在控制台进行输入,切换解释器版本
- 如何进行服务器的批量管理以及python 的paramiko的模块
- iOS利用CoreLocation获取地理位置以及如何在模拟器进行调试
- 利用Python中的输入和输出功能进行读取和写入的教程
- 利用python进行数据分析——p26,"一定要以pylab模式”打开如何解决
- 数据库事务的四大特性、隔离级别以及Spring中如何利用AOP进行事务管理
- python-8-如何进行反向迭代以及如何实现反向迭代?
- python3 - 对有规律的字符串进行切割(split用法)
- iOS利用CoreLocation获取地理位置以及如何在模拟器进行调试
- 如何利用深度学习写诗歌(使用Python进行文本生成)
- 如何利用深度学习写诗歌(使用Python进行文本生成)
- 输入空格如何进行控制