How to Use Input Function in Python: Simple Guide
In Python, use the
input() function to get text input from the user. It waits for the user to type something and press Enter, then returns that text as a string.Syntax
The input() function can be used with or without a prompt message inside the parentheses.
input(): Waits for user input without showing a message.input('Your message: '): Shows a message to guide the user before input.
The function always returns the input as a string.
python
input([prompt])Example
This example asks the user for their name and then greets them using the input they typed.
python
name = input('What is your name? ') print('Hello, ' + name + '!')
Output
What is your name? Alice
Hello, Alice!
Common Pitfalls
One common mistake is expecting input() to return a number directly. It always returns a string, so you must convert it if you want a number.
Another mistake is forgetting that the program waits for input and seems to pause.
python
age = input('Enter your age: ') print(age + 5) # This will cause an error because age is a string # Correct way: age = int(input('Enter your age: ')) print(age + 5)
Output
Enter your age: 20
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: can only concatenate str (not "int") to str
Enter your age: 20
25
Quick Reference
| Usage | Description |
|---|---|
| input() | Waits for user input without prompt |
| input('Prompt: ') | Shows prompt message before input |
| int(input()) | Converts input string to integer |
| float(input()) | Converts input string to float |
| str(input()) | Ensures input is treated as string (default) |
Key Takeaways
Use
input() to get user input as a string in Python.Always convert input to the needed type, like int or float, before calculations.
You can add a prompt message inside
input() to guide the user.Remember that
input() pauses the program until the user types and presses Enter.