Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of Print() function in Python

Detailed explanation of the usage of Print() function in Python

DDD
DDDOriginal
2023-09-08 13:33:372665browse

Usage of Print() function in Python: 1. Output multiple values. The print() function can output multiple values ​​at the same time. You only need to pass multiple values ​​as parameters to the print() function; 2. Format formatted output, the print() function also supports formatted output, and you can use placeholders to specify the output format; 3. Modify the output delimiter and end character, the print() function can modify the delimiter and end character through the sep and end parameters. character; 4. Redirect output, the print() function can output the content to a file.

Detailed explanation of the usage of Print() function in Python

The print() function in Python is a very commonly used function. It is used to output the specified content to the standard output device, usually the console. In this article, I will introduce the usage of print() function in detail.

Basic usage

The basic usage of the print() function is very simple. You only need to pass the content to be output as a parameter to the print() function. For example:

print("Hello, World!")

In this example, the print() function outputs the string "Hello, World!" to the console.

1. Output multiple values

The print() function can output multiple values ​​at the same time. You only need to pass multiple values ​​to the print() function as parameters and separate them with commas. For example:

name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.")

In this example, the print() function prints the string "My name is", the value of the variable name, the string "and I am", the value of the variable age and the string "years old. "Output to the console in turn.

2. Formatted output

The print() function also supports formatted output, and you can use placeholders to specify the output format. Commonly used placeholders include %s, %d, %f, etc. For example:

name = "John"
age = 25
print("My name is %s and I am %d years old." % (name, age))

In this example, %s represents the string placeholder and %d represents the integer placeholder. The print() function outputs the string "My name is", the value of the variable name, the string "and I am", the value of the variable age and the string "years old." to the console in the specified format.

3. Modify the output delimiter and terminator

The print() function uses spaces as the delimiter and newlines as the terminator by default. However, we can modify the separator and terminator through the sep and end parameters. For example:

name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.", sep="-", end="!")

In this example, sep="-" means using "-" as the separator, and end="!" means using "!" as the terminator. The print() function outputs the string "My name is-John-and I am-25 years old.!" to the console in the specified format.

4. Redirect output

In addition to outputting to the console, the print() function can also output the content to a file. The output file can be specified through the file parameter. For example:

name = "John"
age = 25
with open("output.txt", "w") as f:
    print("My name is", name, "and I am", age, "years old.", file=f)

In this example, the print() function prints the string "My name is", the value of the variable name, the string "and I am", the value of the variable age and the string "years old. "Output to a file named output.txt in the specified format.

Summary

This article introduces the usage of print() function in detail. You can use the print() function flexibly for output through basic usage, outputting multiple values, formatting output, modifying the output delimiter and terminator, and redirecting output. The print() function is very useful when debugging and outputting results, and is one of the commonly used functions in Python.

The above is the detailed content of Detailed explanation of the usage of Print() function in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn