Python Getting User Input
Table of Contents + β
In the last lesson you learned Type Conversion, where you turned one kind of value into another. Now we put that to real use. Up to now your programs decided everything ahead of time. The values were written right into the code. But a real program needs to listen to the person using it. So how do you ask a question and read back whatever they type?
π€ Why Read User Input?
Think about any app you use. WhatsApp asks for your phone number. Netflix asks who is watching. A program that cannot ask questions can only ever do the one thing it was written to do.
Here is the problem. Your code is fixed, but people are different. One person types βAlexβ. The next types βRiyaβ. You cannot write every name in the world into your code.
The fix is one small function. input() stops the program and waits. The person types something and presses Enter. Then it hands that text back to you.
π§© What input() Does
input() is Python asking the keyboard a question and waiting for the answer.
Here is the simplest possible use. This asks for a name and stores whatever was typed.
name = input("What is your name? ")print("Hello, " + name)The text inside the brackets, "What is your name? ", is the prompt. It is the message shown to the person so they know what to type. When they type Alex and press Enter, that word gets stored in name. Then print greets them.
If you run this and type Alex, you see this.
Output
What is your name? Alex Hello, Alex
See how the program paused at the question? That pause is the whole point. Nothing else runs until the person answers.
Tip
Always end your prompt with a space, like "What is your name? ". Without it the typed answer sticks right against the question and looks messy.
π§± The One Rule You Must Remember
Here is the thing that confuses almost everyone the first time.
input() always gives you back a string. Always. Even if the person clearly types a number.
So if someone types 25, you do not get the number twenty-five. You get the text "25". It looks like a number to your eyes. But to Python it is just characters, like letters.
Let me show you why this matters. This little program asks for an age and tries to add one year.
age = input("How old are you? ")print(age + 1)You would expect it to take 25 and print 26, right? But run it and type 25, and Python crashes with this.
Output
How old are you? 25Traceback (most recent call last): File "main.py", line 2, in <module> print(age + 1)TypeError: can only concatenate str (not "int") to strPython is telling you it cannot add the number 1 to the text "25". They are different types. The text "25" and the number 25 are not the same thing to Python.
π§ Fixing It With Conversion
This is exactly where the last lesson comes back. You convert the text into a real number first.
Wrap the input in int() to turn the typed text into a whole number. Use float() instead if you need decimals.
age = int(input("How old are you? "))print("Next year you will be", age + 1)Read the first line from the inside out. First input(...) reads the text, like "25". Then int(...) wraps around it and turns "25" into the real number 25. Now age holds an actual number, so the math works.
Type 25 and you get this.
Output
How old are you? 25 Next year you will be 26
So the habit is simple. If you need to do math on what someone typed, convert it the moment you read it.
Caution
Only convert when the value really is a number. If someone types hello and you call int("hello"), Python crashes because that text is not a number. Names stay as strings. Ages and prices become int or float.
πͺ The Four Steps Every Time
Reading input follows the same little rhythm every single time. Once you see it, you will spot it in every program.
-
Show a prompt
Ask a clear question so the person knows what to type. The prompt is the text you pass into
input().input("Enter the price: ") -
Store the answer
Save whatever they typed into a variable so you can use it later.
price = input("Enter the price: ") -
Convert if needed
If you will do math with it, change the text into a number with
int()orfloat(). For plain text like a name, skip this step.price = float(input("Enter the price: ")) -
Use it
Now do whatever you wanted with the value, like a calculation or a message.
print("With tax:", price * 1.1)
π§βπ» A Real Example: Name and Age
Let us put the whole rhythm together in one small program. It asks for a name, asks for an age, then prints a friendly message that uses both.
name = input("What is your name? ")age = int(input("How old are you? "))
print("Nice to meet you,", name)print("In 10 years you will be", age + 10)Walk through it line by line.
- The first line reads the name and keeps it as a string, because a name is text.
- The second line reads the age and wraps it in
int(), so we can do math on it. - The third line greets the person by name.
- The last line adds
10to the age, which only works because we converted it.
Type Riya and 25, and you see this.
Output
What is your name? Riya How old are you? 25 Nice to meet you, Riya In 10 years you will be 35
Notice the mix. The name stayed a string and the age became a number. That choice depends on what you plan to do with each value.
β οΈ Common Mistakes
A few traps catch people again and again. Here is what to watch for.
- Doing math on the raw input without converting it first. This is the big one.
# β Avoid: age is still text "25", so this crashesage = input("Age: ")print(age + 1)
# β
Good: convert to a number firstage = int(input("Age: "))print(age + 1)- Forgetting the space at the end of the prompt, so the answer crowds the question.
# β Avoid: prints "Name:Alex" with no gapname = input("Name:")
# β
Good: clean gap before the answername = input("Name: ")- Converting text that is not a number. Only call
int()orfloat()on values that really are numbers, or Python will crash.
β Best Practices
Keep these small habits and input becomes easy.
- Write a clear prompt that says exactly what you want. βEnter your age in years: β beats a bare βAge?β.
- Convert right at the moment you read, like
int(input(...)). It keeps the number-handling in one place. - Keep text as text. Names, cities, and messages do not need converting.
- Use
int()for whole numbers andfloat()when decimals are possible, like a price.
π§© What Youβve Learned
- β
input()pauses your program and reads whatever the person types. - β
The text inside
input("...")is the prompt that tells the person what to enter. - β
input()always returns a string, even when the person types a number. - β
You convert that string with
int()orfloat()before doing any math. - β The rhythm is always the same: prompt, store, convert if needed, then use.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What type of value does input() always return?
Why: input() always returns a string, even if the person types a number like 25.
- 2
A user types 25 for `age = input("Age: ")`. What does `age + 1` do?
Why: age holds the text "25", and Python cannot add the number 1 to a string, so it raises a TypeError.
- 3
How do you read an age and use it as a whole number for math?
Why: int() wraps the input and turns the typed text into a real whole number you can do math with.
- 4
Which value should you usually keep as a string and NOT convert?
Why: A name is plain text, so it stays a string; you only convert values you plan to do math with.
π Whatβs Next?
Now you can read names and numbers from anyone using your program. Next you will learn a clean, modern way to drop those values straight into your messages instead of stitching them together with commas and plus signs.