In this tutorial, learn how to change variable type in Python. The short answer is to use the available functions in Python like int()
, float()
, str()
, tuple()
, etc.
So, let’s start converting variable types using Python with examples given here.
1 2 3 4 5 6 7 8 9 10 11 |
#different type conversion a = int(4.3) b = float(12) c = int('3') d = str(9) #print result print(a) print(b) print(c) print(d) |
Output
12.0
3
9
Convert Float to Integer Variable Type in Python
To change float type variable into an integer, you have to use the Python int()
. The function takes a single argument as the float variable to convert to integer.
1 2 3 4 5 |
#change float to integer a = int(9.6) #print result print(a) |
Output
The above example showing the converted float variable to the integer type. The given number is 9.6 that is of float type. When it is converted to an integer, the digits after the decimal (.) are removed from the number. After the conversion, you will get 9 as the integer type number. This is just because of the int()
function.
int()
of Python.Change Integer to Float Variable Type Using Python
If you want to change the integer type variable into a float, you can use the Python float()
function. It takes an integer variable as an argument to convert to float.
1 2 3 4 5 |
#change integer to float a = float(4) #print result print(a) |
Output
The above example shows the converted integer variable to float type. The given number is 4 and it is of integer type. When you convert it to float type, it adds a decimal place to the number. After the conversion, you will get 4.0 which is a float type number.
How to Change Number to String Variable Type
To change any number to a string type variable, you have to use the str()
function. The function takes the number as an argument to change to string type. See the example below that shows the number and the method to convert to string.
1 2 3 4 5 |
#change number to string a = str(13) #print result print(a) |
Output
The output in the above example shows the string type result.
String to Tuple Conversion in Python
For string to tuple conversion, you have to use the tuple()
in Python. The function takes the string as an argument to convert to a tuple. It breaks the string into single comma-separated letter as showing below:
1 2 3 4 5 6 7 8 |
#declare string in Python a = 'Python' #string to Tuple conversion myTuple = tuple(a) #print result print(myTuple) |
Output
The above example shows single-quoted letters that are comma-separated. Each letter or character is the element of a tuple variable.
Read More: How to Create Tuple Variable in Python
Convert String to Set Conversion in Python
When you want to convert string to set variable type in Python, you have to use the set()
function. It takes a single argument as the string. It breaks the string characters into single comma-separated characters within curly brackets ({}
).
1 2 3 4 5 6 7 8 |
#declare string in Python a = 'Python' #string to Set conversion mySet = set(a) #print result print(mySet) |
Output
The above example shows the converted string type into set variable type.
Change String to List Conversion in Python
A list is a collection of elements of similar data type enclosed within square brackets ([]
). You can change the string type to list using the list()
function of Python. The function split a string into comma-separated single characters placed within the square brackets ([]
) as given below:
1 2 3 4 5 6 7 8 |
#declare string in Python a = 'Python' #string to List conversion myList = list(a) #print result print(myList) |
Output
The above example showing the output that contains the list after the conversion
Read More: Create List Variable in Python With Examples
I hope you like this tutorial on how to change variable types using Python.