# Day 9 – Type Casting in Python: Explicit & Implicit Type Conversion

![](https://cdn.hashnode.com/uploads/covers/664f77938fc1f806b829b90b/9451d23c-f4d9-4424-bfa7-b6f54e021f27.jpg align="center")

## What is Type Casting?

**Type casting**, also called **type conversion**, is the process of converting a value from one data type to another.

For example, a value stored as a string can be converted into an integer:

```python
a = "10"
b = int(a)

print(b)
print(type(b))
```

Output:

```text
10
<class 'int'>
```

Python provides several built-in functions that can be used for type conversion, including:

| Function | Converts a value to |
| --- | --- |
| `int()` | Integer |
| `float()` | Floating-point number |
| `str()` | String |
| `bool()` | Boolean |
| `list()` | List |
| `tuple()` | Tuple |
| `set()` | Set |
| `dict()` | Dictionary, when the input has a valid dictionary-compatible structure |
| `ord()` | Unicode code point of a single character |
| `hex()` | Hexadecimal string representation of an integer |
| `oct()` | Octal string representation of an integer |

* * *

# Types of Type Conversion

Python type conversion can generally be understood in two ways:

1.  **Explicit Type Conversion**
    
2.  **Implicit Type Conversion**
    

* * *

## 1\. Explicit Type Conversion

**Explicit type conversion** happens when the programmer manually converts a value from one data type to another.

This is also commonly called **explicit type casting**.

Python's built-in conversion functions such as `int()`, `float()`, and `str()` can be used for this purpose.

### Example

```python
string = "15"
number = 7

string_number = int(string)

total = number + string_number

print("The sum of both numbers is:", total)
```

Output:

```text
The sum of both numbers is: 22
```

Here:

```python
string = "15"
```

is a string.

We explicitly convert it to an integer using:

```python
string_number = int(string)
```

Now the addition can be performed between two integers.

### Important

The string must contain a valid integer representation.

For example:

```python
int("15")
```

works, but:

```python
int("hello")
```

raises a `ValueError`.

Similarly:

```python
int("15.5")
```

also raises a `ValueError` because `"15.5"` is not a valid integer literal for `int()`.

* * *

## Common Explicit Conversions

### String to Integer

```python
a = "10"
b = int(a)

print(b)
print(type(b))
```

Output:

```text
10
<class 'int'>
```

### Integer to Float

```python
a = 10
b = float(a)

print(b)
print(type(b))
```

Output:

```text
10.0
<class 'float'>
```

### Integer to String

```python
a = 100
b = str(a)

print(b)
print(type(b))
```

Output:

```text
100
<class 'str'>
```

* * *

# 2\. Implicit Type Conversion

**Implicit type conversion** happens when Python automatically converts a value to another compatible type during an operation.

For example:

```python
a = 7
b = 3.0

c = a + b

print(c)
print(type(c))
```

Output:

```text
10.0
<class 'float'>
```

Here:

*   `a` is an `int`.
    
*   `b` is a `float`.
    
*   Python converts the integer value `7` to a floating-point value for the addition.
    
*   The result is a `float`.
    

Conceptually:

```text
7 + 3.0
↓
7.0 + 3.0
↓
10.0
```

This happens automatically, so we do not need to call `float()` ourselves.

* * *

## Why Does Python Do This?

Python performs certain automatic conversions when doing so is appropriate and does not require losing information.

For example:

```python
a = 7
b = 3.0

print(a + b)
```

produces:

```text
10.0
```

The result is a float because the operation involves a floating-point value.

However, Python does **not** automatically convert unrelated types in every situation.

For example:

```python
a = "1"
b = 2

print(a + b)
```

This raises a `TypeError`.

Python does not automatically convert `"1"` into `1`.

We must explicitly convert it:

```python
a = "1"
b = 2

print(int(a) + b)
```

Output:

```text
3
```

* * *

# Explicit vs Implicit Type Conversion

| Feature | Explicit Conversion | Implicit Conversion |
| --- | --- | --- |
| Who performs it? | Programmer | Python |
| Also called | Type casting | Automatic type conversion |
| Requires conversion function? | Usually | No |
| Example | `int("10")` | `10 + 2.5` |
| Control | Programmer controls the conversion | Python performs the conversion |

* * *

# Important Examples

### Example 1: Strings

```python
a = "1"
b = "2"

print(a + b)
```

Output:

```text
12
```

Why?

Both values are strings, so `+` performs **string concatenation**.

It does not perform numerical addition.

### Example 2: Explicit Conversion

```python
a = "1"
b = "2"

print(int(a) + int(b))
```

Output:

```text
3
```

Here we explicitly convert both strings into integers.

### Example 3: Implicit Conversion

```python
c = 1.9
d = 8

print(c + d)
print(type(c + d))
```

Output:

```text
9.9
<class 'float'>
```

Python automatically handles the integer and float combination, producing a float result.

* * *

# Key Takeaways

1.  **Type casting** means converting a value from one data type to another.
    
2.  **Explicit conversion** is performed manually by the programmer.
    
3.  Functions such as `int()`, `float()`, and `str()` are commonly used for explicit conversion.
    
4.  **Implicit conversion** is performed automatically by Python in certain compatible operations.
    
5.  `"1" + "2"` produces `"12"` because both values are strings.
    
6.  `int("1") + int("2")` produces `3`.
    
7.  `1 + 2.5` produces `3.5` because the integer participates in a floating-point operation.
    
8.  Python does not automatically convert unrelated types such as `str` and `int` during `+`.
    
9.  Type conversion is important when working with user input, calculations, files, APIs, and data processing.
    

* * *

## Quick Revision

```text
Type Casting
│
├── Explicit Conversion
│   ├── int()
│   ├── float()
│   ├── str()
│   ├── list()
│   ├── tuple()
│   └── set()
│
└── Implicit Conversion
    └── Python automatically performs
        certain compatible conversions
```

### Remember

```python
"1" + "2"          # "12"
int("1") + int("2") # 3
1 + 2.5            # 3.5
```

Type casting becomes especially useful when handling values coming from sources such as `input()`, files, APIs, databases, and user-entered data.

* * *

## **📂 Day 9 Resources**

All notes and code for this day are available in the GitHub repository:

%[https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/09-Day09-Typecasting-in-Python] 

* * *
