Module 2 · Python Syntax and Fundamentals — topic 13 of 14
Type conversion
Convert values between types with int(), float(), str() and bool(): when Python converts automatically, when you must convert yourself, and what each conversion does to the value.
Learning objectives
- Convert between int, float, str and bool
- Explain implicit versus explicit conversion
- Predict the result and errors of common conversions
Definition
Convert values between types with int(), float(), str() and bool(): when Python converts automatically, when you must convert yourself, and what each conversion does to the value.
Introduction
Values have types, and some operations only make sense for certain types. Type conversion turns a value of one type into an equivalent value of another, such as the text "25" into the number 25.
Why it matters
Input from users and files arrives as text, while calculations need numbers, and messages need text again. Converting at the right moments is part of almost every program you will write.
Detailed explanation
Implicit conversion: Python converts automatically only when it is safe and obvious. Mixing int and float in arithmetic gives a float: 3 + 0.5 is 3.5.
Explicit conversion: you call a type’s function. int("42") → 42, float("2.5") → 2.5, str(99) → "99".
int() of a float drops the fractional part (truncates toward zero): int(7.9) is 7 and int(-7.9) is -7. Use round() if you want rounding.
int() of a string requires a whole number written in digits; int("3.5") raises ValueError. Use int(float("3.5")) if needed.
bool() turns 0, 0.0, empty strings and None into False and almost everything else into True. Note that bool("False") is True, because it is a non-empty string.
Python never converts text to numbers implicitly: "5" + 5 is an error, not 10 or "55".
Conversion always creates a new value. After age = int(age_text), age_text is still the original string; only the new name refers to the number. This is why you usually assign the converted result to a variable and use that variable from then on.
Syntax
- Each function returns a new value; the original is unchanged.
- Invalid input raises ValueError.
- The type names are also the conversion functions.
Code examples
Common conversions
50 5.0 77 9
Truthiness with bool()
False True False True False
More examples
Converting data from a text record
Kiran is 18 next year Score out of 10: 8.25 Fees paid: True bool
Step-by-step explanation
- split(",") breaks the record into four strings.
- int() and float() turn the numeric parts into numbers for arithmetic.
- paid is created by a comparison rather than bool(), because bool("no") would be True.
- type(paid).__name__ confirms the result is a Boolean.
Expected output
Running “Converting data from a text record” prints exactly:
Practical applications
- CSV and form data are strings that must be converted before calculating totals.
- Prices are converted to str with formatting when building invoices.
- Configuration values such as "true"/"false" are compared explicitly, not passed to bool().
Best practices
- Convert at the boundary: as soon as data enters or just before it leaves.
- Use round() when you mean rounding, not int().
- Compare text explicitly for yes/no values.
Common mistakes
Converting a decimal string with int()
Avoid
Prefer
int() only accepts strings that look like whole numbers.
Using bool() on yes/no text
Avoid
Prefer
bool() of any non-empty string is True, regardless of what it says.
Key takeaways
- int(), float(), str() and bool() convert values explicitly.
- Python converts implicitly only between numeric types.
- int() truncates floats; int("3.5") is an error.
Try it yourself
- 1Read a price as text, convert it, add 18% tax and print it with two decimals.
- 2Predict and check: int(-2.7), float("10"), str(3.0), bool(" ").
Finished reading? Mark it complete to update your dashboard.
Suggested practice
2 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat does int("3.7") do?