Table of Contents
Everything we’ve written so far has been one-directional. Python talks, you listen. It prints something, you read it. But real programs don’t work like that — they have a conversation. They ask, you answer, they respond.
That’s what input is about. And in Python, getting input from the user is genuinely one of the simplest things you’ll do. One function. That’s it.
input() Function
Just like print() displays output, input() collects input. Here’s the most basic usage:
name = input("What is your name? ")
print("Hello, " + name + "!")
Run this, and Python will pause and wait. A prompt will appear in the terminal saying What is your name? — and the program won’t move forward until you type something and press Enter.
Whatever you type gets stored in the variable name. Then the next line uses it to greet you. If you type Aryan, it prints — Hello, Aryan! Simple, but it’s the beginning of real interactivity.
Notice the space before the closing quote in the prompt — “What is your name? “. That space just makes the prompt look cleaner when the cursor appears next to it. Small detail, but worth the habit.
Important Catch — input() Always Returns a String
Here’s something that catches almost every beginner. No matter what the user types — a name, a number, anything — input() always gives you back a string.
So if someone types 25, Python doesn’t see the number 25 — it sees the text “25”. Try doing maths with it and things go wrong fast:
age = input("Enter your age: ")
print(age + 5) # Error! Cannot add a string and an integer
To fix this, you need to convert the input into the right type. Python calls this type conversion or casting.
Type Conversion — int() and float()
If you’re expecting a whole number from the user, wrap the input in int():
age = int(input("Enter your age: "))
print(age + 5) # Now this works perfectly
If you’re expecting a decimal number, use float():
price = float(input("Enter the price: "))
print(price * 1.18) # Applying 18% tax
And if you need to go the other way — converting a number back to a string — use str():
age = 25
print("I am " + str(age) + " years old.")
Without str() in that last example, Python would complain about trying to concatenate a string and an integer. Always make sure the types match when combining values.
Putting It All Together — A Mini Program
Let’s write a small program that uses everything we’ve covered — input, type conversion, and a bit of arithmetic:
name = input("What is your name? ")
age = int(input("How old are you? "))
year = 2024
birth_year = year - age
print("Hello, " + name + "!")
print("You were born around " + str(birth_year) + ".")
Run that. Type your name and age. Watch Python do the calculation and respond back.
This is only 6 lines, but think about what’s happening — your program is collecting data, processing it, and giving back a personalised response. That’s the core loop of most applications in the world, just at a much larger scale.
Quick Recap
- input() pauses the program and waits for the user to type something
- Always returns a string — no matter what was typed
- Use int() to convert to a whole number, float() for decimals
- Use str() to convert a number back to a string for concatenation
- Type mismatches cause errors — always convert before doing maths
Your programs can now talk and listen. That’s a big deal. With variables, data types, operators, and input all in place — the next natural step is making your program make decisions. That means if-statements and conditions, which is where things start to feel genuinely powerful.
