YouTip LogoYouTip

Python3 Func Input

# Python3.x Python3 input() Function [![Image 3: Python3 Built-in Functions](#) Python3 Built-in Functions](#) The `input()` function is used to get user input from standard input (keyboard) and returns a **string (str) type**. > **Note:** `input()` always returns a string, regardless of what is entered. If numerical calculations are needed, manual type conversion (e.g., `int()`, `float()`) is required. ### Function Syntax input() Parameter Description: * **prompt:** Optional, the message to prompt the user for input. ### Example `input()` gets user input and returns it as a string. ## Basic Usage of input() >>>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 Issue (Common Pitfall) >>>a = input("Please enter a number: ") Please enter a number: 123>>>type(a) **Note:** Values obtained directly from input() cannot be used in numerical calculations. **Incorrect Example:** >>> a = input("Please enter a number: ")>>> print(a + 1)TypeError: can only concatenate str (not "int") to str **Correct Approach:** >>> a = int(input("Please enter a number: "))>>> print(a + 1) ### input() Receiving Multiple Values `split()` will split the input by spaces into an array of strings. ## Example # Input 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) Using `map()` can complete the type conversion in one step, making the code more concise. ## Recommended Approach (Advanced) # One-step split + conversion a, b, c =map(int,input("Please enter three numbers: ").split()) print(a, b, c) ## Practical Example: Calculate Triangle Area # Input the three sides of a 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')) Output: Please enter the lengths of the three sides of the triangle: 3 4 5The area of the triangle is: 6.00 ### Summary of Common Uses * `input("prompt")`: Input with a prompt message * `input()`: Input without a prompt message * `int(input())`: Get an integer * `float(input())`: Get a floating-point number * `input().split()`: Get multiple strings * `map(int, input().split())`: Get multiple integers (Recommended) * * * ### Difference Between Python2 and Python3 In Python2: * `raw_input()`: Returns a string * `input()`: Evaluates the expression (security risk) In Python3: * Only `input()` is retained * Uniformly returns strings, safer and easier to understand ### Core Summary **One-sentence summary:** input() = Get input + Return string * Returns a string by default * Type conversion is required for calculations * For multiple inputs, using split() + map() is recommended * * Python3 Built-in Functions](#)
← Prop Element ChildrenRef Set Symmetric_Difference_U β†’