1.2.14 · D5Introduction to Programming (Python)

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

1,425 words6 min readBack to topic

The one fact everything here orbits: whatever the user types, input() hands you a ==str== (text). Doing arithmetic on text is where every trap springs. If any word below feels new, revisit the parent: [[1.2.14 Input with input() — always returns string, conversion needed (Hinglish)|the topic note]].


True or false — justify

Each answer must explain why, not just say true/false.

input() returns an int when the user types digits like 42.
False. It returns the string "42". The keyboard sends characters, and Python cannot read your intent — it stores the text and waits for you to convert.
input() returns a str even when the user types nothing and just presses Enter.
True. Typing nothing gives the empty string "", still a str. You never escape the string return type.
"3" + "4" and 3 + 4 produce the same value.
False. "3" + "4" is "34" (text joined), while 3 + 4 is 7 (numbers added). Same + symbol, different meaning by type — see String Concatenation vs Arithmetic.
int(input(...)) will always succeed.
False. It only works if the typed text looks like a whole number. "twenty" or "3.5" both raise ValueError.
float("42") works even though 42 has no decimal point.
True. float accepts any numeric-looking text and simply gives 42.0; the decimal point in the source is optional.
int("3.14") gives 3 by rounding down.
False. int() refuses decimal-looking text and raises ValueError. To get 3 you must do int(float("3.14")).
The prompt argument to input() changes the type of what is returned.
False. The prompt is only text printed to the screen. Whether you pass a prompt or not, the return value is always a str.
input() and print() are opposites: one reads text, one shows text.
True. input() pulls a string in from the user; print() pushes values out to the screen. They are the two ends of console I/O.
Converting at the end (just before printing) is equivalent to converting at read time.
False in practice. It technically produces the same value once, but if you use the variable in several places you will keep re-converting and eventually forget one, creating a silent bug.

Spot the error

Say what breaks and how to fix it.

age = input("Age: ") then print(age + 1).
age is a string, so age + 1 mixes str and intTypeError. Fix: age = int(input("Age: ")) so age is a number before adding.
total = int(input("First: ")) + input("Second: ").
Only the first input is converted; the second is still a str, so you add int + strTypeError. Wrap both: int(input("Second: ")).
n = input("Count: ") then print("ab" * n).
n is a string, and you cannot multiply text by text → TypeError. Fix: n = int(input("Count: ")) so the repeat count is a real integer.
price = int(input("Price: ")) where the user types 19.99.
int() rejects text with a decimal point → ValueError. Money has fractions, so use float(input("Price: ")) instead.
x = float(input()) then treating x as an exact whole number for indexing a list.
float gives 19.0, a decimal type; list indices must be int, so mylist[x] raises TypeError. Use int() when the value is a whole count.
data = int(input("Enter: ")) with no safeguard, user types hello.
int("hello") raises an uncaught ValueError and the program crashes. Wrap it in try/except — see Exceptions and try-except.
print("Sum:", input("a: ") + input("b: ")) expecting a numeric sum.
Both inputs are strings, so + concatenates instead of adding: typing 2 and 3 prints Sum: 23, not 5. Convert each with int().

Why questions

Explain the underlying reason.

Why does input() return a string instead of guessing the type for you?
Because keystrokes are raw characters with no attached intent — 5 could mean the number five or a digit in a password. Python gives you the safe raw material and lets you decide the meaning.
Why does "3" + "4" give "34" and not throw an error?
For strings, + is a valid, well-defined operation called concatenation (gluing). It is not an error — it is just a different operation than arithmetic addition. This is why the + operator is called overloaded (Operators in Python).
Why should you convert immediately at read time rather than later?
A single clean line like x = int(input(...)) guarantees x is a number everywhere it is used, removing the chance of forgetting a conversion in one branch of the code.
Why does int("3.14") fail while float("3.14") succeeds?
int promises a whole number and treats a decimal point as invalid text for that promise; float is built to accept fractional text, so it parses it happily. The converter must match the shape of the data — see Type Conversion (Casting).
Why can a program using int(input()) pass all your tests yet crash for a real user?
You always type valid numbers while testing, but a real user might type letters or blanks, triggering ValueError. Untested edge inputs are exactly where the crash hides.
Why does choosing float vs int depend on the meaning of the data, not just what works?
A price can be 19.99 (needs float), but a quantity is a whole count like 3 (needs int). Picking the converter that matches meaning keeps the value honest and prevents subtle rounding or type errors later.

Edge cases

The scenarios the naive story never showed you.

User presses Enter without typing — what does int(input()) do?
input() returns "" (empty string), and int("") raises ValueError because empty text is not a valid whole number. Guard empty input explicitly.
User types 42 with surrounding spaces — does int() still work?
Yes. int() and float() strip leading and trailing whitespace, so int(" 42 ") gives 42. Interior or non-space junk, however, still fails.
User types +5 or -5 — valid for int()?
Yes. A leading sign is part of a valid integer literal, so int("+5") is 5 and int("-5") is -5.
User types 1,000 (with a comma) expecting one thousand.
int("1,000") raises ValueError — Python does not treat commas as thousands separators. You would have to remove the comma first with string processing.
User types 3.0 and you use int(input()).
This raises ValueError because the text contains a decimal point, even though the value is whole. Use int(float("3.0")) to safely get 3.
User types 0 — is that a "falsy" trap?
int("0") is the number 0, which is perfectly valid. But note input() returning "0" (a non-empty string) is truthy as text, while int("0") is falsy as a number — a subtle type-dependent difference.
Two conversions in one expression, first fails: int(input()) + int(input()), first input is hi.
The program crashes on the first int("hi") with ValueError and never even reads the second input. Errors halt execution at the point they occur.

Recall One-line summary of every trap here

The traps all reduce to one rule ::: input() gives you text; treat it as text until you convert it with int() or float(), and convert only when the text truly matches that type.


Connections

  • Variables and Data Types — the str/int/float types every trap depends on.
  • Type Conversion (Casting) — the int()/float() rules behind the edge cases.
  • String Concatenation vs Arithmetic — why "3"+"4" traps you.
  • Exceptions and try-except — how to survive the ValueError cases.
  • Operators in Python — why + and * mean different things by type.
  • print() Function — the output counterpart used in these examples.