The input() function always hands you back a string โ even when it looks like a number. This is where that quietly breaks your math, and why.
This topic covers Python's built-in input() function โ how it works, how to use prompts with it, and (most importantly) the fact that input() always returns a string, no matter what the user types. This has real consequences when you try to do arithmetic on user input, which is demonstrated with a "number concatenation" bug.
input() reads a line of text from standard input (keyboard) and returns it as a string.prompt=).56), Python stores it as a string '56', not an integer.+ on two input strings concatenates them instead of adding them mathematically.type() is used to check the data type of any variable or value.input() โ no promptinput()
56 โ the cell returns:Out[1]: '56'
Note: Even though56looks like a number, it is returned as the string'56'(notice the quotes in the output).
You can pass a custom prompt so the user knows what to type:
input("Apna naam bata")
Apna naam bata (cursor waits right after this text for the user to type)NitishApna naam bataNitish
Out[1]: 'Nitish'
Note how the prompt text and the user's typed input appear on the same line โ input() does not automatically add a space or newline after the prompt.
The same can be written using the keyword argument form:
input(prompt="Apna naam bata")
Produces the identical result: Out[1]: 'Nitish'
This is the most important gotcha demonstrated in this lesson.
first_num = input("Enter the first number") # user enters: 56
second_num = input("Enter the second number") # user enters: 76
print(first_num) # 56
print(second_num) # 76
Now, adding them:
result = first_num + second_num
print(result)
Output:
5676
first_num and second_num are both strings ('56' and '76'), the + operator performs string concatenation, not addition โ so '56' + '76' becomes '5676'.This demonstrates why you must convert input to the correct type (e.g., using int() or float()) before doing math with it โ showing the fix, this is the natural next step (type conversion).
type()The lesson uses type() repeatedly to prove/inspect data types:
| Expression | Result | Explanation |
|---|---|---|
type(4) | int | A plain integer literal |
type(True) | bool | Boolean values have their own type |
type([1,2,3,4]) | list | Lists are their own type |
type(first_num) | str | Confirms that even numeric-looking input from input() is a string |
first_num (direct output) | '56' | Quotes in output confirm it's a string, not the number 56 |
type(first_num)
# Out: str
first_num
# Out: '56'
# Basic input, no prompt
value = input()
# Input with a prompt (positional argument)
value = input("Enter something: ")
# Input with a prompt (keyword argument)
value = input(prompt="Enter something: ")
# Checking type
type(value) # always returns <class 'str'> for input()
input() always returns a string, regardless of what the user types (numbers, True/False, etc. all become plain text).int(input("Enter a number")) (this is the natural next lesson: type conversion).+ on strings concatenates; it does not add numeric values unless both operands have first been converted to int/float.prompt parameter of input() can be passed positionally or by keyword (prompt=) โ both work identically.type() any time you're unsure what data type a variable actually holds โ don't assume from appearance alone.input() needs an interactive frontend; it will error out in non-interactive/unsupported execution contexts.input() reads from the keyboard and always gives back a string.input("...") or input(prompt="...") โ same result.input() + input() on two "numbers" glues them together as text (e.g., "56" + "76" โ "5676"), it doesn't add them.type() is your tool to double-check what type a value really is โ don't trust appearances (a number-looking string is still str).int()), which is the natural next topic after this one.input() reads from the keyboard and always gives back a string. Prompt text can go inside input("...") or input(prompt="...") โ same result. Classic beginner bug: input() + input() on two "numbers" glues them together as text instead of adding them, because both sides are strings. Use type() to double-check what type a value really is, and convert with int()/float() before doing math on user input.