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.
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:
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:
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'>
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
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.
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:
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: