---
title: Type Casting in Python
source: https://app.sythra.ai/learn/python/type-casting
topic: Python
updated: 2026-08-12
publisher: Sythra (https://app.sythra.ai)
---

# 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).

_Source: [https://app.sythra.ai/learn/python/type-casting](https://app.sythra.ai/learn/python/type-casting) — free to read on Sythra._

## Key points

- Implicit casting happens automatically when Python combines compatible numeric types
- Explicit casting uses int(), float(), str(), bool() and more
- int("Hello") raises ValueError — casting must make logical sense
- int() truncates decimals, it does not round
- bool() follows Python's truthiness rules: 0, "", and empty containers are falsy

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](/learn/python/data-types).

## What you will learn

- The difference between **implicit** and **explicit** casting
- How to convert between `int`, `float`, `str`, and `bool`
- Why some conversions raise `TypeError` or `ValueError`
- 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.

```python
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:

```python
age = "20"
print(age + 5)
# TypeError: can only concatenate str (not "int") to str
```

You 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()

```python
age = "20"
age = int(age)

print(age + 5)
# 25
```

Now Python understands `age` as a number, so addition works.

### Converting to float — float()

```python
price = "99.99"
price = float(price)

print(price)
# 99.99
```

### Converting to string — str()

```python
score = 100
message = "Your score is " + str(score)

print(message)
# Your score is 100
```

Here 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:

```python
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

```python
bool(0)       # False
bool(1)       # True
bool("")      # False
bool("Hi")    # True
```

Python 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

```python
int(3.9)
# 3
```

`int()` 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.

> **Worth knowing:** 

## 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 by `ValueError`
- Forgetting that `bool("False")` is `True` — any non-empty string is truthy, even the string `"False"`
- Relying on implicit casting between incompatible types like `str` and `int`, which Python will never do for you

## Why this matters

Mastering type casting means fewer `TypeError`s, cleaner data handling — especially with user input, which always arrives as a string — and better control over exactly how your program interprets values.

> **Practice with Sythra:**  [Practice with AI tutor](https://app.sythra.ai/pricing)

## FAQ

### 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.

## Related

- [Data Types in Python](https://app.sythra.ai/learn/python/data-types) — The full map of built-in types this page converts between.
- [Types, Values, and Errors in Python](https://app.sythra.ai/learn/python/types-values-errors) — How type() works and what values and types mean.
- [Variables in Python](https://app.sythra.ai/learn/python/variables) — Names that point at the values you cast here.
- [The input() Function in Python](https://app.sythra.ai/learn/python/python-input-function) — Why input() always returns a string that often needs casting.
- [Functions in Python: Definition and How They Work](https://app.sythra.ai/learn/python/functions-in-python) — What functions are, def, parameters, and local scope.
- [Python course hub](https://app.sythra.ai/learn/python) — All free Python explainers and the path into Agentic practice.
- [Built-in Functions in Python](https://app.sythra.ai/learn/python/built-in-functions-python) — type(), len(), range(), and the functions always available.
- [Try/Except and Exception Handling in Python](https://app.sythra.ai/learn/python/try-except-python) — Catching errors gracefully with try, except, else, finally.

---

Written by Sythra — Learn machine learning by building. Practice this topic with Sythra's AI tutor: https://app.sythra.ai/pricing
