---
title: "What is Python Casting with example?"  
description: "What is Python Casting with example?"  
author: "ICSM Computer"  
published: 2025-09-15  
updated: 2025-09-15  
canonical: https://www.mindstick.com/interview/34372/what-is-python-casting-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 3 minutes  

---

# What is Python Casting with example?

> **Python**, *casting* (also called *type conversion*) means converting a value from one data type to another.

## There are two main types:

## 1. Implicit Casting (Type Conversion)

Done automatically by Python (no data loss).

For example, when combining an integer with a float:

```python
x = 5      # int
y = 2.5    # float

z = x + y  # Python automatically converts `x` to float
print(z)   # 7.5
print(type(z))  # <class 'float'>
```

## 2. Explicit Casting (Type Conversion)

Done manually by the programmer using functions:

- `int()` → converts to integer
- `float()` → converts to floating-point number
- `str()` → converts to string
- `bool()` → converts to boolean

### Examples:

```python
# String to integer
a = int("10")
print(a, type(a))   # 10 <class 'int'>

# Float to integer (decimal part is truncated)
b = int(9.8)
print(b, type(b))   # 9 <class 'int'>

# Integer to float
c = float(5)
print(c, type(c))   # 5.0 <class 'float'>

# Number to string
d = str(123)
print(d, type(d))   # "123" <class 'str'>

# String to float
e = float("3.14")
print(e, type(e))   # 3.14 <class 'float'>

# Any non-zero number becomes True
f = bool(1)
print(f, type(f))   # True <class 'bool'>

# Empty string or 0 becomes False
g = bool("")
print(g, type(g))   # False <class 'bool'>
```

## Summary Table of Casting Functions in Python:

| Function | Converts To | Example |
| --- | --- | --- |
| `int(x)` | Integer | `int("12") → 12` |
| `float(x)` | Floating number | `float("3.5") → 3.5` |
| `str(x)` | String | `str(99) → "99"` |
| `bool(x)` | Boolean | `bool(0) → False` |

## Answers

### Answer by ICSM Computer

> **Python**, *casting* (also called *type conversion*) means converting a value from one data type to another.

## There are two main types:

## 1. Implicit Casting (Type Conversion)

Done automatically by Python (no data loss).

For example, when combining an integer with a float:

```python
x = 5      # int
y = 2.5    # float

z = x + y  # Python automatically converts `x` to float
print(z)   # 7.5
print(type(z))  # <class 'float'>
```

## 2. Explicit Casting (Type Conversion)

Done manually by the programmer using functions:

- `int()` → converts to integer
- `float()` → converts to floating-point number
- `str()` → converts to string
- `bool()` → converts to boolean

### Examples:

```python
# String to integer
a = int("10")
print(a, type(a))   # 10 <class 'int'>

# Float to integer (decimal part is truncated)
b = int(9.8)
print(b, type(b))   # 9 <class 'int'>

# Integer to float
c = float(5)
print(c, type(c))   # 5.0 <class 'float'>

# Number to string
d = str(123)
print(d, type(d))   # "123" <class 'str'>

# String to float
e = float("3.14")
print(e, type(e))   # 3.14 <class 'float'>

# Any non-zero number becomes True
f = bool(1)
print(f, type(f))   # True <class 'bool'>

# Empty string or 0 becomes False
g = bool("")
print(g, type(g))   # False <class 'bool'>
```

## Summary Table of Casting Functions in Python:

| Function | Converts To | Example |
| --- | --- | --- |
| `int(x)` | Integer | `int("12") → 12` |
| `float(x)` | Floating number | `float("3.5") → 3.5` |
| `str(x)` | String | `str(99) → "99"` |
| `bool(x)` | Boolean | `bool(0) → False` |


---

Original Source: https://www.mindstick.com/interview/34372/what-is-python-casting-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
