python常用方法

83 阅读1分钟

输入

单行输入

a = input("")

a = input().split(' ') // 输入默认为str类型
>> 1 2 3 4 
>>['1', '2', '3', '4']

a = list(map(int, input().split(' '))) 
>> 1 2 3 4
>> [1, 2, 3, 4]

a = int(input().strip()) // 默认用法:去除空格 
str.strip() 去除字符串两边的空格 
str.lstrip() 去除字符串左边的空格
str.rstrip() 去除字符串右边的空格 
注:此处的空格包含'\n', '\r', '\t', ' '

多行输入

Python读取多行键盘输入_python多行输入-CSDN博客

输出

print('abc' + 'edf')

animal = 'cat'
num = 4
print('A %s has %d legs' % (animal, num))
print('A {0} has {1} legs'.format(animal='cat', num=4))

print('%.2f' % 3.1415) // 保留小数点后两位

c = input('请输出单个字符')
print(c + "的ASCⅡ码为", ord(c))

end的用法 默认为换行符

a, b = 0, 1
while a < 1000:
    print(a, end='') // 表示输出不换行
    a, b = b, a + b

sep的用法 分隔符,默认为空格

print('abc', 'edf', sep=',') // sep默认为空格