Skip to content

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.

Beginner 14 min read·Lesson 27 of 306 in Python Programming

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

Python
int(value)
float(value)
str(value)
bool(value)
  • 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

Python
print(int("42") + 8)
print(float("2.5") * 2)
print(str(7) + "7")
print(int(9.99))
Output
50
5.0
77
9

Truthiness with bool()

Python
print(bool(0), bool(15), bool(""), bool("False"), bool(None))
Output
False True False True False

More examples

Converting data from a text record

Python
record = "Kiran,17,82.5,yes"
name, age_text, score_text, paid_text = record.split(",")
age = int(age_text)
score = float(score_text)
paid = paid_text == "yes"
print(name, "is", age + 1, "next year")
print("Score out of 10:", score / 10)
print("Fees paid:", paid, type(paid).__name__)
Output
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:

TextOutput
Kiran is 18 next year
Score out of 10: 8.25
Fees paid: True bool

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

int("7.5")

Prefer

float("7.5") # or int(float("7.5"))

int() only accepts strings that look like whole numbers.

Using bool() on yes/no text

Avoid

subscribed = bool("no") # True!

Prefer

subscribed = answer == "yes"

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

  1. 1
    Read a price as text, convert it, add 18% tax and print it with two decimals.
  2. 2
    Predict 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?
Practise this topic