python特殊输入输出

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41668995/article/details/88840961

输入:

1、输入为int或float型的

因有些网站玄武盾会屏蔽eval这个字符串,因此最好用int或float

a=int(input())
b=float(input()) 
c=eval(input()) #不确定准确的数据类型,自动判断可转换为int或float

2、一行输入多个元素

python的输入是输入的字符串,所以在输入后要转换类型

①以空格分隔,eg,2  3.4  4

a,b,c=input().split(" ")
a=int(a)
b=float(b)
c=eval(c)

②用map函数,以逗号,分隔,eg,2,3,4

适用于一行的数据类型都相同

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

输出:

1、保留小数点后几位

x=3.1234567
print("%.2f"%x)

2、占位

x=33
print("%3d"%x)

3、取消输出时的回车

print("3214", end="")

同理,若想要输出有字符串,eg,";"。则可

print("3214", end=";")

4、取消输出时逗号,产生的空格

x=23
y=45
print(x, y, sep = '')

5、输出完整的一句话中有变量

exceed=60
print("Exceed %d%%."%exceed) #%%为转义百分号%

或用format

hour=3
minute=9
print("时间为:",format(hour, "d"),format(minute, "02d"), sep = '')

猜你喜欢

转载自blog.csdn.net/weixin_41668995/article/details/88840961