它是一种字符串格式化语法(它是从 C 中借用的)。
例子:
name = input("What is your name? ")
print("Hello %s, nice to meet you!" % name)
这就是程序的样子:
What is your name? Johnny
Hello Johnny, nice to meet you!
您也可以通过字符串浓度来做到这一点...
name = input("What is your name? ")
print("Hello + name + ", nice to meet you!")
...但是,格式化通常更容易。
您也可以将它与 多个 字符串一起使用。
name = input("What is your name? ")
age = input("How old are you? ")
gender = ("Are you a male or a female? ")
print("Is this correct?")
print("Your name is %s, you are %s years old, and you are %s." % name, age, gender)
输出:
What is your name? Johnny
How old are you? 37
Are you a male or a female? male
Is this correct?
Your name is Johnny, you are 37 years old, and you are male.
现在,在问任何更多问题之前,请看看它们是否存在!