1.2.14 · HinglishIntroduction to Programming (Python)

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

1,625 words7 min readRead in English

1.2.14 · Coding › Introduction to Programming (Python)


input() KYA hai?

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

Yeh key fact yaad kar lo: input() ki return value hamesha str type ki hoti hai.

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

WHY yeh hamesha ek string return karta hai?

Toh Python yeh soch ke chalta hai ki "programmer ko raw material do; unhe khud shape karne do."


HOW convert karna hai (fix)

Tum result ko ek type-conversion function mein wrap karte ho:

Tumhe chahiye Use karo Example
integer int() int("42")42
decimal float() float("3.14")3.14
text rakhna (kuch nahi) "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 zaroori hai — woh failure jo yeh rokta hai

Agar tum conversion skip karke math karne ki koshish karo, toh ya toh galat answer milega ya crash hoga.

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" texts ko saath jod deta hai → "34". + operator strings ke liye join matlab rakhta hai lekin numbers ke liye add. Same symbol, type ke hisaab se alag meaning.


Worked Examples


Forecast-then-Verify

Recall Run karne se pehle output predict karo
x = input("?")   # user types 10
y = input("?")   # user types 20
print(x + y)

Tumhara forecast? ... Answer: 1020 — dono strings hain, + concatenate karta hai. Agar tumne 30 predict kiya, toh tum classic trap mein fanse. 30 pane ke liye: print(int(x) + int(y)).


Common Mistakes (Steel-manned)


Active Recall



Recall Feynman: ek 12-saal ke bacche ko samjhao

Socho tumne apne dost se kaha ke ek number sticky note par likhe aur tumhe de. Tumhara dost "5" likhta hai aur tumhe kagaz deta hai. Kagaz number 5 nahi hai — yeh ek kagaz ka tukda hai jispar 5 ki drawing hai! Agar tum do aise kagaz add karna chahte ho, toh tum unhe sirf saath chipka nahi sakte (tumhe "54" milega, jo bakwaas hai). Pehle tumhe har kagaz padhna hoga aur drawing ko apne dimag mein real number mein badalna hoga. Python mein, input() tumhe kagaz (ek string) deta hai, aur int() woh hai jab tum uspar likhe number ko "padhte aur samajhte" ho.


Flashcards

input() hamesha kaun sa data type return karta hai?
Ek string (str), chahe user kuch bhi type kare.
Input ko whole number mein kaise convert karte hain?
Wrap karo: int(input("...")).
Input ko decimal number mein kaise convert karte hain?
Wrap karo: float(input("...")).
Python mein "3" + "4" kya evaluate karta hai?
"34" — string concatenation, 7 nahi.
input() number ki jagah string kyun return karta hai?
Yeh raw keyboard characters read karta hai aur tumhara intent nahi jaanta, isliye raw text deta hai convert karne ke liye.
int("hello") kaun si error raise karta hai?
ValueError — text ek valid integer nahi hai.
"ab" * "3" kya karta hai?
TypeError raise karta hai; pehle "3" ko int mein convert karna hoga.
Best practice: input kab convert karni chahiye?
Turant jab padhte ho, jaise n = int(input(...)).
42 type karne ke baad print(type(input())) kya dikhata hai?
<class 'str'>.

Connections

  • Variables and Data Typesstr, int, float yahan involved types hain.
  • Type Conversion (Casting)int(), float(), str() functions detail mein.
  • String Concatenation vs Arithmetic — kyun + overloaded hai.
  • print() Functioninput() ka output counterpart.
  • Exceptions and try-exceptint(input()) ko bad input (ValueError) se guard karna.
  • Operators in Python — operator behavior operand types par depend karta hai.

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))