1.标准输入
x=input(“Please enter your input:”)
>>>a=eval(input(())
4*5+6
>>>a
26
2.标准输出
printf([输出项1,输出项2,…,输出项n][,sep=分隔符][,end=jieshufu])
3.格式化输出
print()格式说明
d或i | 带符号的十进制整数 |
---|---|
o | 八进制无符号整数 |
x或X | 十六进制无符号整数,x小写形式输出a,b,c,d,e,f的十六进制数,X大写输出 |
c | 输出一个字符 |
s | 输出字符串 |
f | 小数,默认输出六位 |
e或E | 指数形式输出整数 |
g或G | 自动选择f与e中较紧凑的一种格式,不输出无意义的0 |
附加格式说明符
m | 域宽,十进制整数,m大于实际位数,前面补空格,小于实际位数,按实际位数输出 |
---|---|
n | 附加域宽,十进制,指定小数部分的输出位数 |
- | 左对齐 |
+ | 输出正数时,以+号开头 |
>>print('%04d-%02d-%02d'%(year,month,day)) #缺位补0
2019-09-24
>>>print('%.2e'%128.88888)
1.29e+02
4.字符串的format方法
(1)使用“{格式}”形式的格式说明符
>>>"{} {}".format("hello" ,“world”)
'hello world'
>>>"{0} {1}".format("hello" ,“world”)
'hello world'
>>>"{1} {0} {1}".format("hello" ,“world”)
'world hello world'
(2) “{序号:格式说明符}”形式
>>>"{0:.2f} {1}".format(x=3.1415926,100)
'3.14,100'
".2f"说明项1的输出格式
(3)“{序号:格式说明符}”形式
>>>"pi={x}".format(x=3.14)
'pi=3.14'
(4) “{序号/键:格式说明符}”形式
>>>"{0},pi={x}".format(“圆周率”,x=3.14)
'圆周率,pi=3.14'
格式控制标记
(1) <宽度>
>>>"{0:10}".format("Python")
"Python " #字符串右边以空格补齐。若实际宽度大于10,按实际宽度
(2) <对齐>
>>>"{0:>10}".format("Python") #右对齐
>>>"{0:<10}".format("Python") #左对齐
>>>"{0:^10}".format("Python") #居中对齐
(3) <填充>
>>>"{0:*>10}".format("Python") #右对齐使用*填充
(4),(逗号)
显示千位分隔符,整数浮点数都适用
>>>"{0:,}".format(123456789.12)
123,456,789.12
(5) <.精度>
>>>"{0:.2f} {1}".format(x=3.1415926,100)
'3.14,100'
(6) <类型>
b | 二进制 |
---|---|
c | 整数对应的Unicode字符 |
d | 十进制 |
o | 八进制 |
x | 小写十六进制 |
X | 大写十六进制 |
>>>'{:b}'.format(10)
1010