python有三种字符串占位符方式,代码演示如下:
if __name__ == "__main__":
name = "Lucy"
age = 20
# 方式一:占位符,%s表示只能接受string, %d代表只能接受数字
print("name is %s and age is %d" % (name, age))
# format的用法1,需要用{n}作占位符,占位符从0开始
print("name is {0} and age is {1}".format(name, age))
# format的用法2,需要用{alias(别名)}作占位符
print("name is {_name} and age is {_age}".format(_name=name, _age=age))
输出:
name is Lucy and age is 20
name is Lucy and age is 20
name is Lucy and age is 20