Sitemap
Devikumpatla

Devikumpatla

Type Conversion in Python

4 min readAug 28, 2026

Python is known for its simplicity and flexibility. While working with Python, we often need to convert data from one type to another. For example, a number entered by a user is usually stored as a string, but we may need to convert it into an integer before performing calculations.

This process is called Type Conversion.

What is Type Conversion?

Type Conversion is the process of changing a value from one data type to another.

For example:

```python
x = “100”

y = int(x)

print(y)
print(type(y))
```

Output:

```text
100
<class ‘int’>
```

Here, `”100"` is initially a string. Using `int()`, we converted it into an integer.

Python provides two main types of type conversion:

1. Implicit Type Conversion
2. Explicit Type Conversion

1. Implicit Type Conversion

Implicit Type Conversion occurs when Python automatically converts one data type into another without the programmer explicitly requesting the conversion.

This commonly happens when Python works with compatible numeric data types.

Example:

```python
a = 10
b = 2.5

result = a + b

print(result)
print(t
ype(result))
```

Output:

```text
12.5
<class ‘float’>
```

Here:

* `a` is an `int`
* `b` is a `float`
* Python automatically converts `10` into `10.0`
* The final result becomes a `float`

Conceptually, Python performs:

```text
10 + 2.5
↓

10.0 + 2.5
↓
12.5
```

This automatic conversion helps Python perform the operation without losing the fractional information of the float.

2. Explicit Type Conversion

Explicit Type Conversion occurs when the programmer manually converts a value from one data type to another.

Python provides several built-in functions for this purpose.

Some commonly used functions are:

Function | Purpose
`int()` — Converts a value to an integer
`float()` — Converts a value to a floating-point number
`str()` — Converts a value to a string
‘bool()` — Converts a value to a Boolean
`list()` — Converts an iterable to a list
`tuple()` — Converts an iterable to a tuple

`set()` — Converts an iterable to a set

Let’s understand them with examples.

Converting String to Integer

The `int()` function can convert a numeric string into an integer.

```python
number = “50”

result = int(number)

print(result)
print(type(result))
```

Output:

```text
50
<class ‘int’>
```

Here, `”50"` is a string, but `int()` converts it into the integer `50`.

This is especially useful when taking numbers as input from users.

Example with User Input

```python
age = input(“Enter your age: “)

age = int(age)

print(age + 1)
```

The `input()` function returns a string. Therefore, we use `int()` when we want to perform arithmetic operations.

Converting Integer to Float

The `float()` function converts a value into a floating-point number.

```python
number = 25

result = float(number)

print(result)
print(type(result))
```

Output:

```text
25.0
<class ‘float’>
```

The integer `25` becomes the floating-point value `25.0`.

Converting Float to Integer

The `int()` function can also convert a float into an integer.

```python
price = 99.99

result = int(price)

print(result)
```

Output:

```text
99
```

Notice that Python does **not round** `99.99` to `100`.

Instead, `int()` removes the fractional part.

For example:

```python
print(int(10.8))
print(int(25.4))
```

Output:

```text
10
25
```

— -

Converting Integer to String

The `str()` function converts a value into a string.

Get Devikumpatla’s stories in your inbox

Join Medium for free to get updates from this writer.

```python
age = 22

result = str(age)

print(result)
print(type(result))

```

Output:

```text
22
<class ‘str’>
```

This is useful when we need to combine numbers with text.

For example:

```python
age = 22

message = “My age is “ + str(age)

print(message)
```

Output:

```text
My age is 22
```

Converting String to Float

A numeric string can be converted into a floating-point number using `float()`.

```python
number = “45.75”

result = float(number)

print(result)
print(type(result))
```

Output:

```text
45.75
<class ‘float’>
```

Converting Values to Boolean

The `bool()` function converts a value into either `True` or `False`.

```python
print(bool(1))
print(bool(0))
```

Output:

```text
True
False
```

Generally, values such as `0`, `None`, and empty collections are considered **False**, while many non-empty or non-zero values are considered **True**.

For example:

```python
print(bool(“”))
print(bool(“Python”))
```

Output:

```text
False
True
```

Converting String to List

The `list()` function can convert an iterable into a list.

```python
text = “Python”

result = list(text)

print(result)
```

Output:

```text
[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
```

Each character of the string becomes an individual element in the list.

A Common Type Conversion Error

Type conversion is useful, but not every value can be converted successfully.

For example:

```python
number = “Hello”

result = int(number)
```

This produces a `ValueError` because `”Hello”` is not a valid representation of an integer.

Similarly:

```python
number = “10.5”

result = int(number)
```

also raises a `ValueError`.

If the string represents a decimal number, we can first convert it to a float:

```python
number = “10.5”

result = float(number)

print(result)
```

Output:

```text
10.5
```

Simple Example

Implicit:

```python
a = 10
b = 5.5

print(a + b)
```

Python automatically handles the numeric conversion.

Explicit:

```python
a = “10”

print(int(a) + 5)
```

Here, the programmer explicitly converts the string into an integer.

Why is Type Conversion Important?

Type conversion is an important part of everyday Python programming.

It is commonly used when:

* Taking input from users
* Performing mathematical calculations
* Processing data from files

* Working with databases
* Handling API responses
* Converting data for different operations
* Preparing data for analysis and machine learning

For example, when we use:

```python
age = input(“Enter your age: “)
```

Python stores the entered value as a string.

If we want to perform mathematical operations, we can convert it:

```python
age = int(input(“Enter your age: “))

print(age + 1)
```

This is one of the most common practical uses of type conversion.

--

--