格式化输出 %s %d

name = input("请输入你的名字:")
address = input("请输入你来自哪里:")
wife = input("请输入你的老婆:")
notlike = input("请输入你不喜欢的明星:")

print("我叫"+name+", 我来自"+address+", 我老婆是"+wife+", 我不喜欢"+notlike)
需要掌握的内容===============
# 格式化输出
print("我叫%s, 我来自%s, 我老婆是%s, 我不喜欢%s" % (name, address, wife, notlike))
# 新版本的格式化输出
print(f"我叫{name}, 我来自{address}, 我老婆是{wife}, 我不喜欢{notlike}")
需要掌握的内容===============

hobby = "踢球"
print("我喜欢%s, 我老婆更喜欢%s" % (hobby, hobby))

%s 表示字符串的占位 . 全能的占位.
print("周杰伦今年%s岁了" % 18)
# %d 占位数字. 只能放数字
print("周杰伦去年%d岁了" % 16)
print("周杰伦去年%d岁了" % "16") # 报错

# 坑, 如果这句话使用了格式化输出. % 就是占位, 如果想显示正常的%   %% 转义
# print("我叫%s, 我已经度过了30%的人生了" % "俞洪敏") # not enough arguments for format string
print("我叫%s, 我已经度过了30%%的人生了" % "俞洪敏")

  

猜你喜欢

转载自www.cnblogs.com/WANG-/p/10026352.html