python 格式化输出

1、使用%占位符

使用方式一:

name =input("请输入您的名字")
age = input("请输入您的年龄")
gender = input("请输入您的性别")
print("您的名字是%s,性别是%s,年龄是%s"%(name,gender,age))

使用方式二:

name =input("请输入您的名字")
age = input("请输入您的年龄")
gender = input("请输入您的性别")
print("您的名字是%(1)s,性别是%(2)s,年龄是%(3)s"%{'1':name,'2':gender,'3':age})

2、使用format

name =input("请输入您的名字")
age = input("请输入您的年龄")
gender = input("请输入您的性别")
print("您的名字是{0},您的年龄是{1},您的性别是{2}".format(name,age,gender))
原文地址:https://www.cnblogs.com/linshuhui/p/9415472.html