python中raw_input().strip().split()

来源:互联网 发布:外汇mt4软件下载 编辑:程序博客网 时间:2024/06/07 03:46

1、strip()方法

strip() 方法用于移除字符串头尾指定的字符(默认为空格)。


strip()方法语法:

str.strip([chars])

参数是chars--移除字符串头尾指定的字符.

例子如下:

#!/usr/bin/pythonstr = "HHHH I am a student HHHHH";print str.strip( 'H' );

输出:I am a student


2、split()方法

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


split()方法语法:

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

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

例子如下:

#!/usr/bin/pythonstr = "Line1-abcdef \nLine2-abc \nLine4-abcd";print str.split( );print str.split(' ', 1 );

输出:


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

3、下面举出一个综合例子说明一下:

raw_input().strip().split()效果

raw_input()          #' insert 0 5     'raw_input().strip()  #'insert 0 5'raw_input().strip().strip()  #['insert', '0', '5']

左边是输入,右边为输出结果

参考:http://www.runoob.com/python/att-string-split.html
http://stackoverflow.com/questions/40598078/how-does-raw-input-strip-split-in-python-work-in-this-code

0 0
原创粉丝点击