What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More
Python

Python Type Casting: Converting int, str, and float

Jun 30, 2026 7 Minutes Read Why Trust Us Why you can trust this guide. Written by working engineers and reviewed by our editorial team under a strict editorial policy for accuracy, clarity and zero bias. Kaustubh Saini By Kaustubh Saini Kaustubh Saini Kaustubh Saini
I'm Kaustubh Saini, founder of FavTutor. I have a genuine passion for coding and data science. In my articles, I aim to break down complex topics, share coding insights, and make learning more accessible. When I'm not writing, I'm always exploring ways to enhance your learning experience at FavTutor.
Connect on LinkedIn →
Python Type Casting: Converting int, str, and float

You'll often have a value of one type and need it as another. A string of digits that should really be a number. A number you want to drop into a sentence. A value you want to test as true or false. Turning one type into another is called type casting, or type conversion, and you'll do it constantly in everyday Python.

We'll cover the conversion functions, the difference between conversions Python does for you and ones you ask for, the input case that sends most beginners here, and the errors to watch for. Each part has a short example you can run.

What is type casting?

Type casting means converting a value from one data type to another. You wrap the value in the name of the type you want, like int() or str(), and Python hands you back a new value of that type.

The text 25 being converted by int() into the number 25
text = "25"
number = int(text)
print(number + 1)
# Output: 26

The original value doesn't change. You get a brand new value of the new type. Here text is still the string "25", while number is the integer 25.

The main conversion functions

Each built-in type has a function with the same name that converts a value into it. The four you'll lean on most are int(), float(), str(), and bool().

Conversion functions int, float, str, bool, and list with example results
print(int("7"))      # 7
print(float("3.5"))  # 3.5
print(str(42))       # "42"
print(bool(0))       # False
# Output:
# 7
# 3.5
# 42
# False

You can also convert between containers. For instance, list("abc") gives ['a', 'b', 'c'].

Converting to a string

The str() function turns any value into text. That's what lets you join a number to a message, though an f-string is often cleaner for the same job.

age = 30
print("Age: " + str(age))
print(f"Age: {age}")
# Output:
# Age: 30
# Age: 30

Skip the str() and "Age: " + age raises a TypeError, because you can't add text and a number directly.

Converting to a number

Use int() for whole numbers and float() for decimals. One detail to remember: int() on a float drops the decimal part rather than rounding it.

print(int("100"))    # 100
print(float("9.99")) # 9.99
print(int(7.9))      # 7  (drops the .9, does not round)
# Output:
# 100
# 9.99
# 7

Converting to a boolean

The bool() function follows one simple rule. Empty or zero values become False, and everything else becomes True. So 0, 0.0, "", and empty containers are all "falsy".

print(bool(0))     # False
print(bool(""))    # False
print(bool("hi"))  # True
print(bool(42))    # True
# Output:
# False
# False
# True
# True

Implicit versus explicit conversion

Python sometimes converts types for you. Mix an int and a float, and it quietly promotes the int to a float so the maths works. That's implicit conversion. When you call int() or str() yourself, that's explicit conversion.

Implicit conversion where int is promoted to float, versus explicit conversion using int()
print(2 + 3.0)         # 5.0  (Python promotes 2 to 2.0)
print(int("2") + 3)    # 5    (you converted "2" yourself)
# Output:
# 5.0
# 5

Python won't turn text into a number on its own, though. That part is always your call.

Converting user input

The input() function always returns a string, even when the user types digits. To do maths with it, you convert it first. This is the single most common reason beginners reach for casting.

Input returns text, so adding 1 fails until you wrap it in int()
# age = input("Age: ")     # returns text like "25"
# age + 1                  # TypeError: can't add str and int
# age = int(input("Age: ")) + 1   # works: converts first

The full story on reading input lives in the lesson on the Python input() function.

When conversion fails

Converting only works when the value actually makes sense as the target type. int("42") is fine, but int("abc") raises a ValueError, because "abc" isn't a number.

int of abc raising a ValueError, while int of a digit string converts cleanly
# int("abc")   # ValueError: invalid literal for int() with base 10: 'abc'
text = "42"
if text.isdigit():
    print(int(text) * 2)
# Output: 84

Checking with isdigit() first, or wrapping the call in try and except, keeps your program from crashing on bad input.

Checking the type first

Before converting, you sometimes want to know what you're dealing with. The type() and isinstance() functions tell you a value's current type, which is handy when the source might be a string of Python numbers or actual text.

value = "3.14"
print(type(value))
print(float(value) + 1)
# Output:
# <class 'str'>
# 4.140000000000001

Practice exercises

Run each one and check the output against what you expected.

Text to number

Convert "15" to an integer and add 5.

# Solution
n = int("15")
print(n + 5)
# Output: 20

Number to text

Join a label and a number using str().

# Solution
score = 90
print("Score: " + str(score))
# Output: Score: 90

Truthy or falsy

Print the boolean value of an empty string and a non-empty one.

# Solution
print(bool(""))
print(bool("x"))
# Output:
# False
# True

Common mistakes

  • Adding text and numbers. "Age: " + age fails; convert the number with str() first.
  • Converting non-numeric text. int("ten") raises a ValueError; check with isdigit() or handle the error.
  • Expecting int() to round. int(7.9) is 7, not 8; use round() to round.
  • Forgetting input() returns a string. Always convert before doing maths on it.
  • int() on a decimal string. int("3.5") fails; use float("3.5") first, then int() if needed.

Frequently asked questions

How do I convert a string to an integer in Python?

Wrap it in int(), like int("25"). The string has to hold a valid whole number, or you'll get a ValueError.

How do I convert an integer to a string?

Use str(), so str(25) gives "25". You need this to join a number to text with +.

What is the difference between implicit and explicit conversion?

Implicit conversion happens automatically, like promoting an int to a float in mixed arithmetic. Explicit conversion is when you call int() or str() yourself.

Why does int("3.5") give an error?

Because "3.5" isn't a whole-number string. Convert it with float("3.5") first, then apply int() if you want to drop the decimal.

How do I safely convert user input to a number?

Check the text with isdigit() before converting, or wrap the conversion in try and except to catch a ValueError.

What values are falsy in Python?

0, 0.0, False, None, an empty string "", and empty containers all convert to False. Everything else is truthy.

Key takeaways

  • Type casting converts a value from one type to another and returns a new value.
  • The main functions are int(), float(), str(), and bool().
  • Python does implicit conversion in mixed maths; you do explicit conversion with these functions.
  • Convert input() with int() or float() before doing arithmetic.
  • Converting invalid text raises a ValueError; guard it with isdigit() or error handling.

Casting is the glue between Python's types, and it makes a lot more sense once you know the types themselves well. A good next step is the lesson on Python data types, where each type

Kaustubh Saini
About the author

Kaustubh Saini

I'm Kaustubh Saini, founder of FavTutor. I have a genuine passion for coding and data science. In my articles, I aim to break down complex topics, share coding insights, and make learning more accessible. When I'm not writing, I'm always exploring ways to enhance your learning experience at FavTutor. Connect on LinkedIn →
Up nextPython Strings: Working with Text