Python3 input() Function
input()The function obtains user input from standard input (keyboard) and returns astring (str) type。
Note:
input()Regardless of what is entered, the return value is always a string. If numerical calculations are needed, you must manually perform type conversion (e.g.,int()、float())。
Function Syntax
input([prompt])
Parameter Description:
- prompt:Optional, the message prompting the user for input
Example
input()Get user input and return it as a string.
input() Basic Usage
>>>name = input("Please enter your name:")Please enter your name: Xiao Ming
>>>print("Hello,", name)Hello, Xiao Ming
Even if a number is entered, the return value is still a string.
Type Issues (Common Pitfalls)
>>>a = input("Please enter a number:")Please enter a number:123
>>> type(a)
<class 'str'>
Note:Values obtained directly using input() cannot participate in numerical calculations.
Incorrect Example:
>>> a = input("请输入数字:")
>>> print(a + 1)
TypeError: can only concatenate str (not "int") to str
Correct Approach:
>>> a = int(input("请输入数字:"))
>>> print(a + 1)
input() Receiving Multiple Values
split()Splits the input by spaces into an array of strings.
Example
# Enter multiple values (separated by spaces)
a, b, c = input("Please enter three numbers:").split()
# Convert types
a = int(a)
b = int(b)
c = int(c)
print(a, b, c)
a, b, c = input("Please enter three numbers:").split()
# Convert types
a = int(a)
b = int(b)
c = int(c)
print(a, b, c)
Usemap()can complete type conversion in one step, making the code cleaner.
Recommended Approach (Advanced)
# Complete splitting + conversion in one step
a, b, c = map(int, input("Please enter three numbers:").split())
print(a, b, c)
a, b, c = map(int, input("Please enter three numbers:").split())
print(a, b, c)
Practice: Calculating Triangle Area
# Enter the three sides of the triangle
a, b, c = map(int, input("Please enter the lengths of the three sides of the triangle:").split())
# Calculate semi-perimeter
p = (a + b + c) / 2
# Calculate area using Heron's formula
s = (p * (p - a) * (p - b) * (p - c)) ** 0.5
print("The area of the triangle is:", format(s, '.2f'))
a, b, c = map(int, input("Please enter the lengths of the three sides of the triangle:").split())
# Calculate semi-perimeter
p = (a + b + c) / 2
# Calculate area using Heron's formula
s = (p * (p - a) * (p - b) * (p - c)) ** 0.5
print("The area of the triangle is:", format(s, '.2f'))
Running result:
请输入三角形三边的长:3 4 5 三角形面积为: 6.00
Common Usage Summary
input("提示"): Input with promptinput(): Input without promptint(input()): Get integerfloat(input()): Get floatinput().split(): Get multiple stringsmap(int, input().split()): Get multiple integers (recommended)
Differences between Python2 and Python3
In Python2:
raw_input(): Returns stringinput(): Executes the expression (has security risks)
In Python3:
- Only retained
input() - Uniformly returns a string, safer and easier to understand
Core Summary
One-sentence summary:
input() = 获取输入 + 返回字符串
- Returns a string by default
- Must convert types when participating in calculations
- For multiple inputs, it is recommended to use split() + map()
Other Extensions
Python3 Built-in Functions