Stop 02 · Variables, Data Types & OperatorsTopics · 8/8

User Input & Type Conversion

Part of the Python Roadmap

In short

Programs get far more useful when they ask the user something. The catch: input() always hands you text, even when the user types a number, and forgetting that causes the first real bug most learners meet.

input() always returns a string, so convert it with int(), float() or bool() before doing maths. Converting invalid text raises a ValueError, which you will learn to handle later.

The jaadu slits open a paper envelope and pulls out a small wooden block shaped like a number token, with more sealed envelopes stacked beside.

Think of it like this

Everything arrives in an envelope

Whatever someone posts you, a cheque, a photo or a recipe, it arrives inside an envelope. You have to open it and take the thing out before you can use it. input() always gives you the envelope (a string). int() and float() open it and pull out the actual number.

Watch it happen

Asking for an age

User
input()
int()
Program

Your age:

1/4input() prints the prompt and waits.

The big ideas

What you need to know

input() returns str

Always. Even "42" is text until you convert it. Check with type(value) when unsure.

Converting types

int("42"), float("99.5") and str(42) convert between types. int("12.5") fails, so use float() for decimals.

Bad text raises ValueError

int("abc") or int("") crashes with ValueError. You'll learn to catch it with try/except in a later phase.

bool("False") is True

Any non-empty string is truthy. To read yes/no answers, compare the text: answer.strip().lower() == "yes".

See it in code

Walk through the code

python
1name = input("Your name: ")2age_text = input("Your age: ")3print(type(age_text))             # <class 'str'>4 5age = int(age_text)6print(f"{name}, next year you'll be {age + 1}.")7 8price = float(input("Ticket price (₹): "))9tickets = int(input("How many tickets? "))10print(f"Total: ₹{price * tickets:.2f}")11 12wants_snacks = input("Snacks? (yes/no): ").strip().lower() == "yes"13print("Add popcorn" if wants_snacks else "No snacks")

1/4input shows the prompt and returns what was typed, always as a string.

Read text, convert it to the right type, then calculate. The yes/no check compares text instead of using bool().

Save yourself the pain

Common mistakes

Oops

Writing age = input("Age: ") then age + 1, and getting a TypeError.

Do this instead

Convert first: age = int(input("Age: ")).

Oops

Using int() for prices like 149.50 and getting a ValueError.

Do this instead

Use float() for anything that may have decimals, or Decimal for exact money.

Oops

Using bool(input(...)) for yes/no questions.

Do this instead

bool("no") is True because the string isn't empty. Compare the cleaned text to "yes" instead.

Your mission

Age in days

~20 min

Quick check

The user types 5 when x = input() runs. What is x * 3?

Go deeper

  1. 1Ask for two numbers with input(), convert them and print their sum.
  2. 2Try int('abc') to see the ValueError you get from bad input.
  3. 3Use str() to turn numbers back into text when joining them with strings.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge