Python: Taking User Input & Type Conversion

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.

Overview

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.

Key Concepts

Detailed Explanation

1. Basic input() โ€” no prompt

input()
Out[1]: '56'
Note: Even though 56 looks like a number, it is returned as the string '56' (notice the quotes in the output).

2. Using a prompt message

You can pass a custom prompt so the user knows what to type:

input("Apna naam bata")
Apna 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'

3. The "string concatenation" trap with numeric input

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
โš ๏ธ This is NOT 56 + 76 = 132. Since 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).

4. Verifying types with type()

The lesson uses type() repeatedly to prove/inspect data types:

ExpressionResultExplanation
type(4)intA plain integer literal
type(True)boolBoolean values have their own type
type([1,2,3,4])listLists are their own type
type(first_num)strConfirms 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'

Commands / Syntax Summary

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

Important Points

Things to Remember

Quick Revision

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.