Python不像C语言,java这些强类型语言。输入时会强制要求限定字符的类型。所以Python输入值默认是字符串类型。
下面小结一下几种常用的输入方法
#以空格间隔,输入数字类型
a,b=map(int,input().split())
#以逗号隔开,输入普通类型数据
a,b,c=map(int,input().split(','))
#列表存储方法
line = list(map(int, sys.stdin.readline().strip().split()))
#方法二
line = [int(n) for n in str_in.split()]
以上几种方法是常用的的Python方法,其中sys调用键盘输入一行,
strip()方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
str = "*****this is **string** example....wow!!!*****"
print (str.strip( '*' )) # 指定字符串 *
#结果是 this is **string** example....wow!!!
str = "123abcrunoob321"
print (str.strip( '12' )) # 字符序列为 12
#结果为 3abcrunoob3
split():在字符串上执行 lstrip()和 rstrip(),
rstrip()
删除字符串字符串末尾的空格.
截掉字符串左边的空格或指定字符