In the last activity, we learned how to prompt the user for input and store their response in a variable using input()
. However, there’s an important detail to remember: input()
function always returns the user input as a string. This means if you ask the user to enter a number, you’ll get that number as text, not as something you can use for arithmetic.
Examples
num1 = "5"
num2 = "3"
print(num1 + num2) # Outputs "53", not 8!
print(num1 * num2) # Error, cannot multiply strings
There are a few functions in Python that help us convert strings that contain numbers into numeric types, such as integers and floats.
int()
- Converts a string into an integerfloat()
- Convert a string into a float (decimal)These functions will attempt to convert the string into its corresponding number, and if successful, it will return that number. Remember, some functions can return data. We can then save that returned value to a variable or use it in some other expression.
Examples
# Converting to an integer (whole number)
number = int("32") # Converts the string "32" to the number 32
print(number + 10) # Outputs 42
# Converting to a float (number with a decimal)
decimal_number = float("100.4") # Converts the string "100.4" to 100.4
print(decimal_number + 10.6) # Outputs 111.0
Example Video 1: Get input and convert over two lines
Example Video 2: Get input and convert in one line
What happens if a user enters something unexpected, like trying to convert “abc” into a number? Or what if we try to convert a string that contains a decimal into an integer, like “32.5”?
bad = int("32.5") # This will cause an error!
In this case, we’ll get an error because the int() function cannot handle a number with a decimal—it only works with whole numbers.
int()
float()
print("--- Demo 1 - Age to Months Calculator ---")
# Steps involved in the statements below:
# 1) User types in age
# 2) Convert string input to integer and save it to a new variable
age_str = input("Enter your age: ")
age = int(age_str)
print(f"You are {age} years old. That's {age * 12} months old, you old fart!\n")
input("\nPress enter to continue...")
print("--- Demo 2 - Square Area Calculator ---")
# Shorter, concise way to get input, convert to number, and save to variable
length = float(input("Enter the length of the square: "))
area = length * length
print(f"The area of the square is {area}")
--- Demo 1 - Age to Months Calculator ---
Enter your age: 30
You are 30 years old. That's 360 months old, you old fart!
Press enter to continue...
--- Demo 2 - Square Area Calculator ---
Enter the length of the square: 45
The area of the square is 2025
If writing input, conversion, and variable assignment all in one line feels confusing, it’s completely okay to break it up into multiple steps. In fact, it’s often helpful to write each step on its own line when you’re learning, so you can better understand what’s happening at each stage.
Remember, there’s no rush to write code in fewer lines—what’s important is that the code makes sense to you! Breaking things down into smaller steps can make it easier to spot errors and understand how each part of the program works.