python3输入_python3之输入输出格式

#encoding:utf-8

#有多组输入数据,但组数不确定

while True:

try:

a, b = map(int, input().split(' ')) #以空格隔开

print(a + b) #map(int, list)把每个list的元素转为int型

except EOFError: #捕捉异常

break

#先输入一个整数,告诉我们接下来有多少组数据,然后在输入每组数据的具体值。,

n = int(input())

for temp in range(n): #range(5)等价于list[0, 1, 2, 3, 4]

a, b = map(int, input().split(' '))

print(a + b)

# 有多组输入数据,没有具体的告诉有多少组,但是却知道遇见什么结束

while True:

a, b = map(int, input().split())

if a == 0 and b == 0:

break

else:

print(a + b)

#有多组输入数据,对于每组输入数据的第一个数代表该组数据接下来要输入数据量

while True:

try:

lst1 = list(map(int, input().split()))

n, lst = lst1[0], lst1[1:]

sum = 0 #对后面n个数据求和

for i in range(n):

sum += lst[i]

print(sum)

except EOFError:

break

#把输入的字符串分成单个字符处理

while True:

lst = list(input())

print(lst)

#print输出不换行

print ("Hello ", end='') #输出为: Hello World!

print ("World", end='')

print("!")

#打印字符串

print("His name is%s"%("zhangsan")) #输出为: His name is zhangsan

#打印整数

print("He is%dyears old"%(25)) #输出为: He is 25 years old

#打印浮点数

print("His weight is%f"%(155.5)) #输出为: His weight is 155.500000

#打印浮点数并指定保留小数点位数

print("His weight is%.2f"%(155.5)) #输出为: His weight is 155.50

#指定占位符宽度

print("Name:%10sAge:%3dHeight:%8.2f"%("zhangsan", 25, 155.5))

#输出为: Name: zhangsan Age: 25 Height: 155.50

#指定占位符宽度(左对齐)

print("Name:%-10sAge:%-3dHeight:%-8.2f"%("zhangsan", 25, 155.5))

#输出为: Name:zhangsan Age:25 Height:155.50

#用0当占位符

print("Name:%-10sAge:%03dHeight:%08.2f"%("zhangsan", 25, 155.5))

#输出为: Name:zhangsan Age:025 Height:00155.50

#输出16进制,十进制,8进制整数

n = 100

print("hex =%x, dec =%d, oct =%o"%(n, n, n)) #输出为: hex = 64, dec = 100, oct = 144

#指数输出

print("%.3e" %2016) #输出2.016e+03,也可以写大E

#输出列表

lst = [1,2,3,4,'python']

print (lst) #输出为: [1, 2, 3, 4, 'python']

#format的使用:通过{}和:来代替传统%方式

#使用位置参数

list = ['zhangsan', 20]

print("my name is {}, age is {}".format('zhangsan', 20)) #输出为: my name is zhangsan, age is 20

print("my name is {1}, age is {0}".format(20, 'zhangsan')) #输出为: my name is zhangsan, age is 20

print("my name is {1}, age is {0} and I am {1}".format(20,'zhangsan'))

#输出为: my name is zhangsan, age is 20 and I am zhangsan

print("my name is {}, age is {}".format(*list)) #输出为: my name is zhangsan, age is 20

print("{a[0]}{a[1]}{a[2]}".format(a=["spkk",".","cn"])) #输出spkk.cn

#使用关键字参数(使用字典)

student = {'name': 'zhangsan', 'age': 20}

print("my name is {name}, age is {age}".format(name = 'zhangsan', age = 20))

#输出为: my name is zhangsan, age is 20

print("my name is {name}, age is {age}".format(**student))

#输出为: my name is zhangsan, age is 20

#填充和格式化

print("{:,}".format(123456))#输出1234,56

print("{a:x^8}".format(a="8"))#输出xxx8xxxx,填充x

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值