Python Foundations · Lesson 3 of 12Input and type conversion
input(), int(), float() and validation. Learn through short examples, practical tasks and targeted questions.
← All lessons
Input and conversion
input() always returns text, even if the user types digits. Programs that need to compare or calculate numerical values must convert that string using int() or float(). Test a whole number, a decimal and non-numeric input to discover when conversion can fail.
Watch out: Do not assume the user will enter a valid number: int("hello") raises a ValueError unless the program handles it.
A free TutorSyntax programming lesson. About our teaching approach.
APPLY THE IDEA
Use this skill beyond the example
The value returned by input() is text, even if a student types digits. Predict what happens when you combine two inputs without conversion, and then compare it with adding int() values.
Quick check: Why might input("Age: ") need conversion before arithmetic?
Check: input() returns a string, so int() or another suitable conversion is needed for numerical arithmetic.
Continue with related practice →