How is the print function used in Python?
In Python, you can use the print function to output content. Printing simple strings can be done directly using the print function, as shown below:
print("Hello, World!")
The output is:
Hello, World!
In addition to strings, you can also print other types of data like integers, floating point numbers, etc. You can directly pass these data as arguments to the print function, as shown below:
print(42) # 打印整数
print(3.14) # 打印浮点数
The output is:
42
3.14
To print multiple items, you can pass them as separate arguments to the print function, as shown below:
print("Hello,", "World!")
The output result is:
Hello, World!
The print function can also be used with specific formatting symbols to display the values of variables. For example, %s can be used to represent a string, %d for integers, %f for floating-point numbers, and so on. These formatting symbols can be used in the print statement by passing the corresponding variables as arguments to the print function. Here is an example code:
name = "Alice"
age = 25
print("My name is %s and I am %d years old." % (name, age))
The output of the results is:
My name is Alice and I am 25 years old.
In addition to the common uses mentioned above, the print function also has many other uses, such as setting the separator and end character for printing. You can learn more about the usage of the print function by referring to the Python official documentation or other tutorials.