---
title: "What is type conversion in Python?"  
description: "What is type conversion in Python?"  
author: "Krishnapriya Rajeev"  
published: 2023-04-28  
updated: 2023-05-01  
canonical: https://www.mindstick.com/forum/158066/what-is-type-conversion-in-python  
category: "python"  
tags: ["python"]  
reading_time: 3 minutes  

---

# What is type conversion in Python?

#### What is type conversion in Python?

## Replies

### Reply by Aryan Kumar

In Python, type conversion, also known as type casting, is the process of changing the data type of a variable from one type to another.

For example, if we have a variable that is an integer, we can convert it to a string using the **str()** function:

```python
my_number = 42
my_string = str(my_number)
print(my_string)  # Output: "42"
```

In this example, the **str()** function is used to convert the integer **my_number** to a string, and the resulting string is stored in the variable **my_string**.

Python supports various built-in functions for type conversion such as **int()**, **float()**, **str()**, **bool()**, and others. These functions can be used to convert a variable from one type to another.

It's important to note that not all types can be converted to each other, and some conversions may result in data loss or unexpected behavior. For example, converting a string to an integer may fail if the string contains non-numeric characters. Therefore, it's important to carefully consider the type conversion and its implications before performing it in your code.

### Reply by Krishnapriya Rajeev

We can convert data from one data type to another easily in Python using type conversion functions. Python has two kinds of type conversion; implicit type conversion and explicit type conversion.

In **implicit type conversion**, the data type is automatically converted by the Python interpreter depending on the context. An example of implicit type conversion is as follows:

```plaintext
a = 3			# an integer
print(type(a))

b = 4.5			# a floating point number
print(type(b))

c = a + b
print(c,': ',type(c))

# Output: <class 'int'>
		  <class 'float'>
		  7.5 :  <class 'float'>
```

In this context, the data type of variable a is converted to float. This is not done the other way round (i.e., data type of b converted to int) because it would lead to loss of information.

In **explicit type conversion**, the user manually changes the data type according to their needs. Examples of such conversions are:

```plaintext
a = float(3)			# converting int to float
print(a)
print(type(a))

b = int(7.254)			# converting float to int
print(b)
print(type(b))

c = str(3.413)			# converting float to string
print(c)
print(type(c))

d = float('12.56')		# converting string to float
print(d)
print(type(d))

e = list('mindstick')	# converting string to list
print(e)
print(type(e))

# Output: 3.0
		  <class 'float'>
		  7
		  <class 'int'>
		  3.413
		  <class 'str'>
		  12.56
		  <class 'float'>
		  ['m', 'i', 'n', 'd', 's', 't', 'i', 'c', 'k']
		  <class 'list'>
```


---

Original Source: https://www.mindstick.com/forum/158066/what-is-type-conversion-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
