格式化输出

name = input("请输入你的名字:")
address = input("请输入你来自哪里:")
wife = input("请输入你的老婆:")
notlike = input("请输入你不喜欢的明星:")
格式化输出   %s  %d
print("我叫%s,我来自%s,我老婆是%s,我不喜欢%s" % (name,address,wife,notlike))

  新版本的格式化输出


print(f"我叫{name},我来自{address},我老婆是{wife},我不喜欢{notlike}")

 -------------------------------------------------------------------

hobby = "踢球"
print("我喜欢%s,我老婆更喜欢%s" % (hobby,hobby))
%s  str占位符  适用于字符串,数字,布尔等各种占位
%d 只适用于数字占位符

print("我今年%d岁了"%  18)
print("我今年%d岁了"%  "18")  #报错


print("我叫%s,我已经过了30%的年龄了" % "ywy")    #报错 "not enough arguments for format string"

print("我叫%s,我已经过了30%%的年龄了" % "ywy")  #正确

  

 
原文地址:https://www.cnblogs.com/YangWenYu-6/p/10028269.html