Standard Input and Output in Python

In this rapidly changing era, the demand in the market surges like a tide, highlighting the importance of programming skills! Among many programming languages, Python has won the hearts of numerous learners due to its wide applicability and ease of use.

Standard Input and Output in Python

Python, the “internet celebrity” of the programming world, is simply a savior for beginners and a favorite among experts! With its clear and concise syntax and powerful features, it shines in various fields such as data analysis, artificial intelligence, and web development, easily tackling various challenges.

Most programs can be divided into three main processes: data acquisition, data processing, and data return. Python provides two built-in functions, input() and print(), for standardized input and output.

1. The input() Function

Syntax: str = input(msg)

① str is a string variable used to receive content input from the console.

② msg is a string variable used to prompt the user in the console about what content to input;

③ After inputting data, it will wait for the user to press the Enter key to finish input.

For example:

age = input("Please enter your age:")
print(type(age))

The output in the console will be:

Please enter your age:

The output after entering the age will be:

Please enter your age:15

Note: If msg is not provided, there will be no error, but the user will not know what to input, which is not user-friendly.

2. The print() Function

Syntax: print(value, sep='', end='\n', file=sys.stdout, flush=False)

The parameters value, sep, end, file, and flush can all be omitted. The meanings and usages of each parameter are as follows:

① The value parameter represents the variable or value to be output, with multiple parameters separated by commas.

For example:

print()
print("Sun Wukong", 6, "Monkey", "Big Disciple")

The output will be:


Sun Wukong 6 Monkey Big Disciple

② If you want to separate multiple output contents with a specified character, you can achieve this by setting the sep parameter.

For example:

print("Sun Wukong", 6, "Monkey", "Big Disciple", sep="-")

The output will be:

Sun Wukong-6-Monkey-Big Disciple

③ The print() function automatically adds a newline after the input is finished. If you do not want to add a newline after the output, you can set the end parameter.

For example:

print("Sun Wukong", 6, "Monkey", "Big Disciple")
print("Zhu Bajie", 2, "Pig", "Second Disciple", end="|")
print("Sha Wujing", 3, "Human", "Third Disciple")

The output will be:

Sun Wukong 6 Monkey Big Disciple
Zhu Bajie 2 Pig Second Disciple|Sha Wujing 3 Human Third Disciple

④ The print() function can not only output content to the screen but also to a specified file, which can be set using the file parameter. The default value is sys.stdout, which represents the system’s default standard output, i.e., outputting content to the screen.

For example:

f = open("Journey to the West.txt", "w", encoding="utf-8") # Open Journey to the West.txt, create if it does not exist; overwrite if it does exist.
print('Zhu Bajie carries his wife', file=f) # Output Zhu Bajie carries his wife to Journey to the West.txt file.
f.close() # Close Journey to the West.txt file

The above code will not output content to the console but will write “Zhu Bajie carries his wife” to the Journey to the West.txt file, which is located in the directory of the Python file where the above code resides.

The structure and result screenshot of the above code are as follows:

Standard Input and Output in Python

print() function output to file

Note: When outputting Chinese to a file, set the file encoding to utf-8.

⑤ The flush parameter can be used to control output buffering, and this parameter is generally kept as False for better performance.

Quantitative changes do not necessarily lead to qualitative changes, but quantitative changes will definitely accumulate.

Standard Input and Output in Python

Standard Input and Output in Python

Click to follow the public account below, reply with 【111】 for free to get Python open classes and hundreds of GB of organized learning materials from experts, including but not limited to Python e-books, tutorials, project orders, source code, etc.

Leave a Comment