Type Casting in Python
Type casting in Python means converting a value from one data type to another, either implicitly (Python does it automatically, like int + float) or explicitly (you call int(), float(), str(), or bool() yourself).
Imagine walking into a bank with dollars, but the shop next door only accepts euros. Your money is still money — it just needs to be converted before it can be used. That is exactly what type casting in Python is: converting a value from one data type into another so an operation actually works.
Sometimes Python converts types for you automatically. Sometimes it refuses to guess, and you must convert explicitly. This page covers both, plus every common casting function and the errors you will hit along the way. It builds directly on Data Types in Python.
What you will learn
- The difference between implicit and explicit casting
- How to convert between
int,float,str, andbool - Why some conversions raise
TypeErrororValueError - Python's truthiness rules when casting to
bool - Why
int()truncates instead of rounding
Implicit casting — when Python decides
This happens automatically. When Python sees two different numeric types combined in one expression, it promotes the smaller type to the larger one so no information is lost.
x = 5 # int
y = 2.0 # float
result = x + y
print(result)
print(type(result))
# 7.0
# <class 'float'>x was an integer, y was a float. Python converted x into a float automatically because mixing int and float always produces a float. This is called implicit casting — Python made the decision, not you.
Explicit casting — when you decide
Sometimes Python refuses to guess what you mean:
age = "20"
print(age + 5)
# TypeError: can only concatenate str (not "int") to strYou are trying to add a string and an integer. Python does not assume what you meant — you must explicitly convert the type yourself using a casting function.
Converting to int — int()
age = "20"
age = int(age)
print(age + 5)
# 25Now Python understands age as a number, so addition works.
Converting to float — float()
price = "99.99"
price = float(price)
print(price)
# 99.99Converting to string — str()
score = 100
message = "Your score is " + str(score)
print(message)
# Your score is 100Here an integer is converted into text so it can be joined with another string using +.
When casting fails
Python will not lie for you. If a conversion does not make logical sense, it raises an error instead of guessing:
int("Hello")
# ValueError: invalid literal for int() with base 10: 'Hello'"Hello" cannot logically become a number, so Python raises a ValueError instead of silently producing garbage. Casting must always make sense.
Common casting functions
| Function | Converts to |
|---|---|
| int() | Integer |
| float() | Float |
| str() | String |
| bool() | Boolean |
| list() | List |
| tuple() | Tuple |
| set() | Set |
Casting to bool — Python's truthiness rules
bool(0) # False
bool(1) # True
bool("") # False
bool("Hi") # TruePython has rules for what counts as "truthy" and "falsy". Zero, empty strings, empty lists, and None are all falsy; almost everything else — including any non-empty string — is truthy.
A subtle example: int() truncates, it does not round
int(3.9)
# 3int() does not round to the nearest whole number. It simply removes the decimal part — 3.9 becomes 3, not 4. If you need rounding, use the built-in round() function instead.
Common mistakes
- Trying to add a string and a number directly instead of casting first
- Assuming
int()rounds — it truncates toward zero - Calling
int()on text that isn't a valid number and being surprised byValueError - Forgetting that
bool("False")isTrue— any non-empty string is truthy, even the string"False" - Relying on implicit casting between incompatible types like
strandint, which Python will never do for you
Why this matters
Mastering type casting means fewer TypeErrors, cleaner data handling — especially with user input, which always arrives as a string — and better control over exactly how your program interprets values.
Common questions
What is type casting in Python?
Type casting is converting a value from one data type to another, such as converting the string "20" into the integer 20 with int("20"). Python does some conversions automatically (implicit) and requires others to be explicit.
What is the difference between implicit and explicit type casting?
Implicit casting happens automatically, like Python converting an int to a float when you add them together. Explicit casting requires you to call a function yourself, like int(), float(), or str(), because Python won't guess your intent.
Why does int("Hello") raise an error?
int() can only convert text that represents a valid number. "Hello" has no numeric meaning, so Python raises a ValueError instead of guessing or returning 0.
Does int() round numbers in Python?
No. int() truncates the decimal part instead of rounding — int(3.9) returns 3, not 4. Use the built-in round() function if you need proper rounding.
How do you convert a string to a number in Python?
Use int("42") to convert to an integer or float("3.14") to convert to a float. Both raise ValueError if the string isn't a valid number.
Why is bool("False") True in Python?
bool() converts based on truthiness, not the text itself. Any non-empty string — including the string "False" — is truthy, so bool("False") evaluates to True. Only empty strings are falsy.
Explore
Related topics
Keep going — these sit next to this concept in a real learning path.
Data Types in Python
The full map of built-in types this page converts between.
Types, Values, and Errors in Python
How type() works and what values and types mean.
Variables in Python
Names that point at the values you cast here.
The input() Function in Python
Why input() always returns a string that often needs casting.
Functions in Python: Definition and How They Work
What functions are, def, parameters, and local scope.
Python course hub
All free Python explainers and the path into Agentic practice.
Built-in Functions in Python
type(), len(), range(), and the functions always available.
Try/Except and Exception Handling in Python
Catching errors gracefully with try, except, else, finally.