1.动态交互输入
x = input("请输入一个数字:")
x
请输入一个数字:4
'4' #输出是一个字符,是带引号的
#eval() 去掉引号
x = eval(input("请输入一个数字:"))
x
请输入一个数字:4
4 #输出是一个数字,不带引号的
2.打印输出
print 默认换行,若不想换行:换行控制 end=
print(123,end=" ")
print(456)
123 456
有时候,我们需要一些复杂的输出:比如几个变量一起组合输出
PI = 3.1415926
E = 2.71828
print("PI = ", PI, "E = ", E)
PI = 3.1415926 E = 2.71828
3. 格式化输出方法 format
基本格式:“字符{ 0 }字符{ 1 }字符”.format(v0,v1)
print("PI = {0},E = {0}".format(PI, E)) #将format中第0个元素填充到相应的位置
PI = 3.1415926,E = 3.1415926
4.修饰性输出
# ____3.1415926_____ 进行填充
print("{0:_^20}".format(PI))
print("{0:*<30}".format(PI))
3.1415926*********************
print("{0:&>20,}".format(10000000)) #数据千分位分隔符
&&&&&&&&&&10,000,000
5.浮点数简化输出
print("{0:.2f}".format(PI)) #3.14
print("{0:.1%}".format(0.818727)) #81.9%
print("{0:.2e}".format(0.818727)) #8.19e-01