Python Change Variable Type

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.

Output

4
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.

Output

9

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.

Note: If the number contains any decimal places, it gets removed when you use int() of Python.
Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in 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.

Output

4.0

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.

Output

13

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:

Output

(‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)

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 ({}).

Output

{‘o’, ‘t’, ‘n’, ‘P’, ‘h’, ‘y’}

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:

Output

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

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.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.