python 3种格式化输出

1.格式化输出,占位符

#整型输出%d
print ('整型 %d'% 10)#整型 10

#输出浮点数,小数点后保留两位有效位数
print ('浮点数,小数点后保留两位有效位数 %.2f ' % (1.890))#浮点数,小数点后保留两位有效位数 1.89

#输出字符串%s
print ('字符串 %s' % 'python')

#多个联合输出
name = "python"
b = 100
print("你好%s,你的额度是%d" % (name,b))

2.格式化输出,format

#python的内置函数format写法
print("{1} {0} {1}".format("hello", "world"))  # 设置指定位置,输出结果:'world hello world'
#冒号后的内容是限定输出字符的格式
print("你好{0},你的余额是{1:.2f}".format("python",3.1))#你好python,你的余额是3.10
print("你好{},你的余额是{}".format("python",3.1))#你好python,你的余额是3.1
print("语言:{name}, 受欢迎程度 {level}".format(name="python", level="****"))

3.格式化输出,python3.6之后的语法

#python3.6之后有的语法
name="python"
print(f"the languge is : {name}")
原文地址:https://www.cnblogs.com/AntonioSu/p/11978095.html