Python Getting User Input

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? 25
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(age + 1)
TypeError: can only concatenate str (not "int") to str

Python 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.

  1. 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: ")
  2. Store the answer

    Save whatever they typed into a variable so you can use it later.

    price = input("Enter the price: ")
  3. Convert if needed

    If you will do math with it, change the text into a number with int() or float(). For plain text like a name, skip this step.

    price = float(input("Enter the price: "))
  4. 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 10 to 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 crashes
age = input("Age: ")
print(age + 1)
# βœ… Good: convert to a number first
age = 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 gap
name = input("Name:")
# βœ… Good: clean gap before the answer
name = input("Name: ")
  • Converting text that is not a number. Only call int() or float() 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 and float() 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() or float() 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. 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. 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. 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. 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.

f-Strings

Share & Connect