pytho格式化输出整数小数字符串

python print格式化输出。

  1. 打印字符串
print("His name is %s,Hello"%("Aviad"))
print("His name is {},Hello".format("Tom"))
  1. 打印整数
print ("He is %d years old"%(25))
print ("He is {} years old".format(25))
  1. 打印浮点数
print("账户中还有%f元钱"%f(312.4))
print("账户中还有{}元钱".format(312.4))

4.打印浮点数(指定保留小数点位数)

print ("His height is %.2f m"%(1.83))
print("账户中还有{:.2f}元钱".format(312.4455))

5.指定占位符宽度

print ("Name:%10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))

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

print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))
print('{} and {}'.format('hello','world'))  # 默认左对齐
print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左对齐,取10位右对齐

7.指定占位符(只能用0当占位符?)

print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))

8.科学计数法

format(0.0015,'.2e')

9.多个变量打印

print("大写数量 %d\n小写数量 %d"%(big,small))

你可能感兴趣的:(python)