In Python programming, input (obtaining user-provided data) and output (displaying results to the user) are the most basic and commonly used operations. Python provides the simple and easy-to-use print() and input() functions to implement these functionalities. Whether you need to print a sentence or get the user’s name, age, etc., these two functions are very helpful. This article will help you easily master the usage of these two functions through practical cases.
print() Function: Let the Program “Speak”¶
The print() function outputs content to the screen, making it the first window for communication between us and the program.
1. Basic Usage: Print Text or Numbers¶
The simplest usage is to directly write the content to be output inside print(). For example:
print("Hello, Python!") # Output: Hello, Python!
print(123) # Output: 123
Note: Strings need to be enclosed in quotes (either single or double quotes), while numbers do not require quotes.
2. Output Multiple Parameters: Separate with Commas¶
print() can accept multiple parameters, separated by commas, and they will be automatically connected with spaces during output:
print("Name:", "Xiaoming", "Age:", 18) # Output: Name: Xiaoming Age: 18
If you want to customize the separator, use the sep parameter. For example, change the separator to “-“:
print("Name:", "Xiaoming", "Age:", 18, sep="-") # Output: Name:-Xiaoming-Age:-18
3. Customize the Ending: Change the “End” of Output¶
By default, print() automatically adds a newline after output (ending with \n). To have multiple print() outputs on the same line, use the end parameter to customize the ending:
print("This is the first line", end=" ") # Output: This is the first line (followed by a space, no newline)
print("This is the second line") # Output on the same line as above: This is the first lineThis is the second line
To have all content on one line, set end to an empty string:
print("Hello", end="")
print("World") # Output: HelloWorld
4. Print Variables or Expressions¶
print() can print not only fixed content but also variables or calculation results:
name = "Xiaohong"
score = 95
print(name) # Output: Xiaohong
print(score) # Output: 95
print(name + " score is:", score) # Output: Xiaohong score is: 95
print(2 + 3 * 4) # Output: 14 (calculates the result of the expression)
input() Function: Let the Program “Listen”¶
The input() function is used to get input from the user, such as the user’s name, age, etc. Note: The return value of input() is always a string type. If a number is needed, it must be manually converted to the appropriate type.
1. Basic Usage: Get User Input¶
You can write a prompt message inside the parentheses of input(), for example:
name = input("Please enter your name: ")
print(f"Hello, {name}!") # If the user enters "Xiaoming", the output is: Hello, Xiaoming!
The variable name stores the user’s input, and its type is a string.
2. Type Conversion: Convert String to Number¶
If the user inputs a number (such as age, score), use int() (for integers) or float() (for decimals) to convert:
# Get integer age
age = int(input("Please enter your age: ")) # If the user enters "18", age = 18 (integer type)
print(f"You are {age} years old this year")
# Get decimal height
height = float(input("Please enter your height (meters): ")) # If the user enters "1.75", height = 1.75 (float type)
print(f"Your height is {height} meters")
If the user enters non-numeric input (e.g., “abc” for age), an error will occur. For now, just remember: numeric types need to be converted using int()/float().
3. Multiple Inputs: Get Multiple Values at Once¶
If you need the user to input multiple values (e.g., two numbers), first split the input content using the split() method, then convert the type:
# Enter two numbers separated by spaces
num1, num2 = input("Enter two numbers separated by spaces: ").split() # Split into a list: ["10", "20"]
num1 = int(num1) # Convert to integer: 10
num2 = int(num2) # Convert to integer: 20
print(f"The sum of the two numbers: {num1 + num2}") # Output: The sum of the two numbers: 30
If using commas to separate, specify split(","):
a, b = input("Enter two numbers separated by commas: ").split(",")
a = int(a.strip()) # Remove spaces
b = int(b.strip())
print(a + b) # If the input is "10,20", output: 30
Comprehensive Practice: Personal Information Collection Program¶
Now, combine print() and input() to create a simple program: get the user’s name, age, and height, then output the formatted personal information.
# Collect user information
name = input("Please enter your name: ")
age = int(input("Please enter your age: "))
height = float(input("Please enter your height (meters): "))
# Output information (using f-string to concatenate variables)
print(f"\n===== Personal Information =====")
print(f"Name: {name}")
print(f"Age: {age} years old")
print(f"Height: {height} meters")
print(f"Next year you will be {age + 1} years old, and your height will be approximately {height + 0.01} meters")
Sample input and output:
Please enter your name: Xiaoli
Please enter your age: 20
Please enter your height (meters): 1.75
Output:
===== Personal Information =====
Name: Xiaoli
Age: 20 years old
Height: 1.75 meters
Next year you will be 21 years old, and your height will be approximately 1.76 meters
Summary¶
print()function: Used to output content, supporting text, numbers, variables, and expressions. Customize separators withsepand ending characters withend.input()function: Used to get user input, returns a string type, which needs to be converted to a numeric type usingint()/float().- Key Tip: Type conversion is crucial! Input obtained via
input()must be manually converted to participate in numeric operations.f-stringis convenient for concatenating variables and expressions.
Practice more small examples combining input and output (e.g., calculators, score statistics) to quickly master the usage of these two functions!