When you first start learning Python, your code usually just runs on its own — no questions, no interaction. But what if you want your program to talk to the user, ask questions, and take input?
That’s where the input() function in Python comes in. It allows your program to pause, ask for user input, and then continue running with that information.
Let’s explore how it works with easy examples.
💡 What Is the Input Function in Python?
The input() function is a built-in Python function that allows your program to collect information from the user. When you use it, the program stops and waits for the user to type something and press Enter.
Here’s a simple example:
name = input("Enter your name: ")
print(name)
When you run this code, Python will show:
Enter your name:
Once you type your name (say, John) and hit Enter, the program will print:
John
Pretty cool, right?
The program actually listens to you — that’s the power of input()!
🧩 How Does Input Work?
In the example above, the text inside the parentheses — "Enter your name: " — is called a prompt.
It’s the message Python displays to guide the user on what to type.
Whatever the user enters gets stored inside a variable (in this case, name), and then you can use that variable anywhere in your program.
⚠️ Important: Input Always Returns a String
Here’s a common mistake many beginners make:
The value you get from input() is always a string, even if the user types a number.
For example:
age = input("Enter your age: ")
print(age + 5)
If you run this and type 25, you’ll get an error:
TypeError: can only concatenate str (not "int") to str
That’s because age is stored as a string ("25"), not a number.
So Python gets confused when you try to add a number to a string.
🔢 How to Convert Input to a Number
If you want to use the input value in math operations, you need to convert it into an integer or a float using the built-in conversion functions int() or float().
Example:
age = int(input("Enter your age: "))
print(age + 5)
Now, when you enter 25, the output will be:
30
This works because we converted the input into an integer.
If you expect a decimal number, use float() instead:
price = float(input("Enter the price of the item: "))
print(price * 1.5)
If you enter 2.5, the result will be:
3.75
Here, we created a simple calculator that multiplies the user’s input by 1.5 — like adding tax to a price!
🧠 Try This Mini Project
Let’s combine everything you’ve learned so far into a fun little example that takes a user’s name and age, and prints a personalized message.
name = input("What's your name? ")
age = int(input("How old are you? "))
print("Hi", name, "in 5 years you will be", age + 5)
When you run this:
What's your name? Lucky
How old are you? 25
Hi Lucky in 5 years you will be 30
You just made your Python program interactive and friendly!
💡 Common Beginner Mistakes
-
Forgetting to convert input:
Always remember thatinput()gives you a string. Convert it if you need numbers.
age = int(input("Enter your age: "))
2. Using the wrong concatenation:
You can’t add strings and integers directly.
Use commas in print() to separate them safely.
print("You will be", age + 5, "years old in 5 years.")
3. Incorrect capitalization:
Function names like input(), int(), and float() are case-sensitive. Use lowercase letters.
Quick Recap
| Concept | Description | Example |
|---|---|---|
| input() | Takes user input as a string | name = input("Enter name: ") |
| int() | Converts input to integer | age = int(input("Enter age: ")) |
| float() | Converts input to float | price = float(input("Enter price: ")) |
| print() | Displays output | print("Hello", name) |
🎯 Final Thoughts
The input function in Python is one of the simplest yet most powerful tools for beginners.
It turns your program from a static block of code into something that talks to the user.
Once you master this, you’ll be ready to build small projects like calculators, quiz games, and more interactive applications.
Next up, we’ll explore output formatting in Python — to make your print statements look clean and professional.
If this post helped you, don’t forget to share it with your coding friends or bookmark it for later!
Copyright statement: Unless otherwise indicated, all articles are original to this site, please cite the source when sharing.
Article link:http://pybeginners.com/simplified-python/input-function-in-python-how-to-take-user-input-with-examples/
License agreement:Creative Commons Attribution-NonCommercial 4.0 International License