How to use the print statement in Python?
The print statement is a method in Python used to display information, typically written with the following syntax:
print("要输出的内容")
The “content to be output” here can be a string, a variable, or even a combination of multiple variables. Below are some examples of using the print statement:
Example 1: Output a string.
print("Hello World")
Example 2: Display the value of a variable.
name = "Alice"print(name)
Example 3: Output the values of multiple variables, separated by commas.
age = 18print("My name is", name, "and I am", age, "years old")
Example 4: Use a formatted string to output the values of multiple variables.
print("My name is %s and I am %d years old" % (name, age))
In example 4, %s and %d are placeholders in the formatting string, representing variables of string and integer types respectively. The values to be filled into the placeholders are provided in parentheses following the %. It is important to note that in Python 3.x, print is a function rather than a statement. Therefore, when using the print statement, the content to be output must be placed within parentheses. For example:
print("Hello World")