title: python input and output
description: Use the print() function to output content to the screen:
Python input and output (IO) brief tutorial
When you are new to programming, the first thing you need to understand is "talking to the program" - let the program output things according to your ideas, and then let it understand what you say. Python is designed to be very friendly in this regard, with no complicated syntax threshold.
1. Output Output: Print out the "words" of the program
There is only one core output tool in Python:print()Functions are easy to use.
1.1 Basic single value output
Whether it is a string, a number, a Boolean value, or a simple calculation expression, just put it in parentheses:
1.2 Multi-parameter batch output
If you want to print several contents at the same time, you don’t need to concatenate the strings, just separate the parameters with commas - Python will add space in the middle and newline at the end by default:
1.3 Elegant formatted output f-string
Although it is convenient to use commas for batch printing, to "embed" variables or calculation results into a complete paragraph, f-string (formatted string literal) is the most recommended way of writing in Python 3.6+ - the syntax is intuitive and the code is easy to read:
2. Input: Let the program "listen" to you
Python has only one core input tool:input()function, it will pause the program and wait for you to enter the Enter key in the terminal before continuing execution.
2.1 Basic silent input
Without any parameters, the program will directly wait for your input without any prompts on the terminal:
2.2 Input with friendly prompts
It is best to add a prompt to the user to avoid not knowing what to enter - just insert the prompt textinput()Just parentheses:
2.3 Input type conversion (important and error-prone points)
⚠️ Note: No matter you enter numbers, letters or symbols,input()Always return string type! If you want to use it for mathematical operations, you must manually convert it intoint(integer) orfloat(floating point number):
Core Rules:
input()What you get is alwaysstr, you must first change the type if you want it to count, otherwise there will be problems that give you a headacheTypeError。
3. Quick preparation: variable basics
To store the input or calculation results for repeated use, you have to use variables - it is like a named "storage box" that can store various types of data:
4. Comprehensive example: simple addition, subtraction, multiplication and division calculator
Combine the output, input, variables, and type conversions you just learned, and write a usable gadget to practice:
5. Modern Python IO tips
- Prefer using f-string instead of the old one
%Format orstr.format(), the code is more concise. - Add basic input verification (such as determining whether the input is a number), otherwise the program will easily crash due to incorrect input.
- Try type annotations (Python 3.5+). Although it does not affect the operation, it will make it clearer for yourself and others to read the code:
- If you need more cool terminal interaction (such as colored text, progress bar), you can use a third-party library
richorclick。
Summarize
- ✅
print(): Output any content, support single/multiple parameters, f-string - ✅
input(): Get user input, always return a string, remember to do type conversion - ✅ Variable: "Box with name" to store data
- ✅ Best practices: use f-string more, add hints, and add type annotations

