Blog Post #16: The Gotcha: Why input() Always Gives You a String

In Post #15, we discovered the power of the input() function to make our programs interactive. We successfully read the user’s name and used it in our script. But what happens when we ask for a number? As promised, it’s time to explore the single most common “gotcha” that trips up new Python programmers.

This post will explain why input() always returns a string and introduce you to the essential technique of type casting to convert that string into a number we can use for math.

Demonstrating the Problem: A Broken Calculator

Let’s try to build a very simple program that asks the user for two numbers and then adds them together. Based on what we know, you might write code like this:

print("--- Broken Adding Machine ---")

num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

# Try to add them together
result = num1 + num2

print("The result is:", result)

If you enter 5 for the first number and 7 for the second, you would reasonably expect the result to be 12. But when you run this code, you’ll see this surprising output:

Enter the first number: 5
Enter the second number: 7
The result is: 57

What happened? The input() function doesn’t know you intended to type numbers. It has one job: to capture keystrokes as text. It captured the character “5” and the character “7”. Therefore, it gave your program the string "5" and the string "7".

In Python, when the + operator is used on two strings, it performs concatenation—it joins them together end-to-end. It does not perform mathematical addition.

The Solution: Type Casting with int() and float()

To solve this, we need to explicitly tell Python to convert the string we received from input() into a numerical data type. This process is called type casting or type conversion.

Python gives us simple, built-in functions to do this:

  • int(): This function takes a value (like a string) and does its best to convert it into an integer (int).
  • float(): This function takes a value and tries to convert it into a floating-point number (float).

Let’s see a quick example:

string_number = "100"
actual_number = int(string_number)

# Let's check their types
print("The type of string_number is:", type(string_number))
print("The type of actual_number is:", type(actual_number))

The output will be:

The type of string_number is: <class 'str'>
The type of actual_number is: <class 'int'>

Fixing Our Calculator: The Correct Pattern

The most common and efficient way to handle numerical input is to “wrap” the input() function call directly inside the conversion function.

The pattern looks like this: variable = int(input("Prompt: "))

When Python sees this, it works from the inside out:

  1. The input("Prompt: ") function runs first, getting the string from the user.
  2. The returned string is immediately passed as an argument to the int() function.
  3. The int() function converts the string to an integer.
  4. This final integer value is what gets stored in the variable.

Let’s fix our adding machine. We will use float() because it’s more flexible—it can handle both 5 and 5.5, whereas int("5.5") would cause an error.

print("--- Fixed Adding Machine ---")

# Get user input and immediately cast it to a number
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Now, the + operator will perform mathematical addition
result = num1 + num2

print("The result is:", result)

Now, when we run the program, we get the correct behavior:

Enter the first number: 5
Enter the second number: 7
The result is: 12.0

What’s Next?

You’ve now learned the most important rule of the input() function: it always gives you a string. You’ve also learned the solution: use the int() and float() functions to cast the string into the correct numerical type. This is a fundamental pattern you will use constantly.

In our broken calculator example, we saw that the + operator behaved differently with strings than it did with numbers. This act of joining strings together is called concatenation. In Post #17, we will explore string concatenation in more detail and learn about other, more modern ways to build strings in Python.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment