1.2.14Introduction to Programming (Python)

Input with `input()` — always returns string, conversion needed

1,807 words8 min readdifficulty · medium

WHAT is input()?

name = input("What is your name? ")
print("Hello", name)

The key fact to burn into memory: the return value of input() is always of type str.

x = input("Enter a number: ")   # user types 5
print(type(x))                  # <class 'str'>  ← NOT int!

WHY does it always return a string?

So Python errs on the side of "give the programmer the raw material; let them shape it."


HOW to convert (the fix)

You wrap the result in a type-conversion function:

You want Use Example
integer int() int("42")42
decimal float() float("3.14")3.14
keep text (nothing) "hi" stays "hi"
age = int(input("Enter your age: "))   # convert immediately
print(age + 1)                          # now math works
Figure — Input with `input()` — always returns string, conversion needed

WHY conversion is needed — the failure it prevents

If you skip conversion and try math, you either get the wrong answer or a crash.

a = input("First number: ")    # user types 3  → a = "3"
b = input("Second number: ")   # user types 4  → b = "4"
print(a + b)                    # prints 34, NOT 7  ← string concatenation!

"3" + "4" glues the texts together → "34". The + operator means join for strings but add for numbers. Same symbol, different meaning depending on type.


Worked Examples


Forecast-then-Verify

Recall Predict the output before running
x = input("?")   # user types 10
y = input("?")   # user types 20
print(x + y)

Your forecast? ... Answer: 1020 — both are strings, + concatenates. If you predicted 30, you fell for the classic trap. To get 30: print(int(x) + int(y)).


Common Mistakes (Steel-manned)


Active Recall



Recall Feynman: explain to a 12-year-old

Imagine you ask a friend to write a number on a sticky note and hand it to you. Your friend writes "5" and gives you the paper. The paper is not the number 5 — it's a piece of paper with a drawing of a 5 on it! If you want to add two such papers, you can't just glue them together (you'd get "54", nonsense). You first have to read each paper and turn the drawing back into a real number in your head. In Python, input() hands you the paper (a string), and int() is you "reading and understanding" the number written on it.


Flashcards

input() always returns what data type?
A string (str), no matter what the user types.
How do you convert an input to a whole number?
Wrap it: int(input("...")).
How do you convert an input to a decimal number?
Wrap it: float(input("...")).
What does "3" + "4" evaluate to in Python?
"34" — string concatenation, not 7.
Why does input() return a string and not a number?
It reads raw keyboard characters and can't know your intent, so it gives raw text for you to convert.
What error does int("hello") raise?
ValueError — the text isn't a valid integer.
What does "ab" * "3" do?
Raises TypeError; you must convert "3" to int first.
Best practice: when should you convert input?
Immediately as you read it, e.g. n = int(input(...)).
What does print(type(input())) show after typing 42?
<class 'str'>.

Connections

  • Variables and Data Typesstr, int, float are the types involved here.
  • Type Conversion (Casting)int(), float(), str() functions in depth.
  • String Concatenation vs Arithmetic — why + is overloaded.
  • print() Function — the output counterpart to input().
  • Exceptions and try-except — guarding int(input()) against bad input (ValueError).
  • Operators in Python — operator behavior depends on operand types.

Concept Map

pauses for

always returns

because keys are

used directly in +

wrong result or crash

convert with

convert with

enables

enables

compact pattern

prevents

input(prompt)

user types + Enter

str value

raw characters, no intent

string join e.g. 3+4 = 34

bug

int()

float()

arithmetic works e.g. 3+4 = 7

value = int(input(prompt))

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, input() ek aisa function hai jo user se kuch type karwata hai, lekin yaad rakho — jo bhi type karoge, wo hamesha string (text) ban ke aata hai. Chahe tum 42 likho ya cat, Python uske liye dono bas "text" hain. Iska reason simple hai: keyboard se characters aate hain, aur Python ko nahi pata ki tumhara number arithmetic ke liye hai ya kisi password ka part hai — isliye wo safe rehkar raw text deta hai aur decision tumhare upar chhod deta hai.

Ab problem kahan aati hai? Maan lo tum do numbers add karna chahte ho: a = input() aur b = input(), aur tum a + b karte ho. Tum socho ge answer aayega 7, par actually aayega "34"! Kyunki strings ke liye + ka matlab hota hai jodna (concatenation), na ki addition. Ye sabse common galti hai jo har beginner karta hai.

Fix bilkul aasaan hai — input ko turant convert karo: int(input(...)) agar pura number chahiye, ya float(input(...)) agar decimal (jaise paise, 19.99) chahiye. Best habit ye hai ki jaise hi input lo, usi line mein convert kar do, baad ke liye mat chhodo, warna kahin bhool jaoge.

Ek aur cheez: int() sirf un strings ko convert karta hai jo number jaise dikhte hain. int("twenty") ya int("hi") likhoge to ValueError crash ho jayega. Asli programs mein iske liye try-except lagate hain. Toh mantra yaad rakho — "Input is INk, not INteger" — jo type kiya wo ink (text) hai, number banane ke liye tumhe khud convert karna padega.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections