目录
字符串
格式化输出
形式:
'%[-][+][0][m][.n]格式字符'%表达式
print('%10d'%3) #整数打印
print('%.4f'%3.14159) #保留四位小数
print('%c'%65) #输出对应ASC码
print('%s'%65) #字符串
print('%s'%[1,2,3])
print(str([1,2,3]))
print('%d'%'123') #把字符串当做整数打印,报错
3
3.1416
A
65
[1, 2, 3]
[1, 2, 3]
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\cpname.py", line 92, in <module>
print('%d'%'123')
TypeError: %d format: a real number is required, not str
使用format()方法进行格式化
print("The number {0:,} in hex is:{0:#x}, the number {1} o