How to convert data types in Python 3

Updated on 2026-10-28 14:01:19 13 visits

Introduction

Python is a high-level programming language that uses dynamic typing. This means you don't need to declare the type of a variable before using it. Python can interpret the type of a variable at runtime.

In the world of programming, it is essential to understand how to work with different data types to create effective and efficient programs. In Python 3, data type conversion is a fundamental skill that every programmer must know.

Data type conversion is the process of changing one data type to another, and it is essential for performing operations and functions that require specific data types. With Python 3, dynamic typing means that variables are not assigned to specific data types at compile time. This allows for greater flexibility in writing code, but it also means that programmers must be prepared to change the data type of a variable during the program's execution.

In this tutorial, you will learn how to convert different data types in Python 3, from basic data, such as integers, floats, and strings, to complex data types, such as lists and tuples.

Basic Data Types in Python

Integer to Float Conversion

Integer to float conversion is a common operation in Python 3, especially when performing mathematical calculations that require numbers with decimals. To convert an integer to a float in Python 3, the float() function can be used. You would need to follow these steps:

  1. First, write float(), then place the value you want to convert inside the parentheses.
  2. Then, execute the code so that the conversion is performed.
  3. Important, verify that it has been converted correctly.

Here is a code example:

numero_entero = 10
numero_flotante = float(numero_entero)
print(numero_flotante)

In this example, the integer 10 is converted to a float number 10.0 using the float() function. The float number is printed on the screen using the print() function.

It is important to know that when converting an integer to a float number, a decimal point and a zero at the end of the number are added. This is because in Python, a float number must always have a decimal point to indicate that it is a number with decimals.

info Integer to float conversion is a simple operation in Python 3. This can be useful in different situations where working with numerical data is required.

Float to Integer Conversion

In Python 3, a float number can be converted into an integer number using the int() function. This conversion can be useful in situations where you need to work with integers instead of decimals. Follow these steps:

  1. Write int() and place the float value you want to convert inside the parentheses.
  2. Execute the code so that you can make the conversion.
  3. Finally, verify if the value has been converted correctly.

A code example:

numero_flotante = 10.6
numero_entero = int(numero_flotante)
print(numero_entero)

Here, the float number 10.6 is converted to an integer number 10 using the int() function. The integer number is printed on the screen using the print() function.

info Important to highlight, when converting a float number to an integer number, the decimal part of the number is lost, meaning that the digit is truncated. Therefore, it is possible that information is lost if working with numbers that require decimals.

To round a number to the nearest integer, the round() function can be used, which shares the same functionality as int(). It is important to note that, when rounding a number with a .5 decimal, the result will be the nearest even integer. For example, if we apply round(10.5), we will get 10; while with round(11.5), the result will be 12.

A code example:

numero_flotante = 10.6
numero_entero = round(numero_flotante)
print(numero_entero)

Here, instead of receiving the number 10 on the screen, we will see the integer number 11 written, since we round to the nearest integer.

Integer to String Conversion

The conversion of an integer to a string is useful when you want to combine a number with text, or when you need to print a number on the screen. In Python 3, an integer can be converted to a string using the str() function. Here we show you how and an example:

  1. Start by writing str(), and also placing the numerical value you want to convert inside the parentheses.
  2. Then, execute the code to make the conversion.
  3. Don't forget to verify the value to know if it has been converted properly.

The code example:

numero_ejemplo = 4
string_ejemplo = 'Example number ' + str(numero_ejemplo)
print(string_ejemplo)

In this case, the integer 4 is converted to a string '4' using the str() function, adding it after the string 'Example number '. Additionally, the string is printed on the screen using the print() function.

info With the str() function, it is possible to convert an integer into a string for use in different parts of the program.

Float to String Conversion

In some tasks, it is necessary to convert a float number into a string to be able to manipulate it or display it in a specific way in a program. In Python 3, there is a function used to perform this conversion: str(). To do it, follow these 3 steps, and then you will have an example:

  1. First of all, write str() and place the float value you want to convert inside the parentheses.
  2. Then, execute the code to carry out this conversion.
  3. To finish, verify if the conversion was successful.

And here is a code example to understand it better:

numero_flotante = 3.14159
cadena_caracteres = str(numero_flotante)
print(cadena_caracteres)

In this example you see, the float number 3.14159 is converted into a string using the str() function. The resulting string is then printed on the screen using the print() function.

info Keep in mind that, when converting a float number into a string, information about its numerical nature is lost. Therefore, the resulting string can be used to display the number on the screen or to manipulate it as a string, but it cannot be used to perform numerical operations.

String to Integer Conversion

There are cases where a string containing an integer must be converted into a numerical value to perform arithmetic operations or comparisons. In Python 3, this conversion can be carried out using the int() function. Now we explain how to do it in 3 simple steps, and of course, with an example:

  1. As always, we start by writing int(), then place the string containing the integer inside the parentheses.

danger Be careful! If the string contains non-numerical characters, an error will occur.

  1. When you finish writing and placing, execute the code to get the conversion.
  2. To finish, you have to verify if the value has been converted correctly.

Here is an example:

cadena_caracteres = '123'
numero_entero = int(cadena_caracteres)
print(numero_entero)

Here, the string '123' is converted into an integer number using the int() function. The resulting number is the one then printed on the screen using the print() function.

info It is worth noting that, when converting from a string to an integer number, any additional information that might be present in the string can be lost. So, if the string contains non-numerical characters or additional information, the conversion will not be possible.

String to Float Conversion

Just like with the conversion from a string to an integer number, it is possible to convert a string containing a number with decimals into a float numerical value in Python 3. To carry out this conversion, you can use the float() function and follow these steps:

  1. Start by writing float() and placing the string containing the number with decimals inside the parentheses.

danger Important! If the string contains non-numerical characters, an error will occur.

  1. Next, execute the code so that the conversion is performed.
  2. Finally, verify if it has been converted correctly.

Additionally, here is a code example:

cadena_caracteres = '3.14'
numero_flotante = float(cadena_caracteres)
print(numero_flotante)

In this case, the string '3.14' is converted into a float number using the float() function. Then, the resulting number is printed on the screen using the print() function.

info Keep in mind that, when converting a string into a float number, any additional information that may be in the string is lost. Thus, if the string contains non-numerical characters or additional information, the conversion will not be possible.

Until now, you have seen basic data conversions in Python. Now we will teach you how to perform more complex data conversions in Python. Let's begin!

Complex Data Types in Python

List to Tuple Conversion

In Python, both lists and tuples are data types used to store collections of elements. Lists are mutable, which means elements can be added, removed, or modified. On the other hand, tuples are immutable, meaning they cannot be modified after their creation. On some occasions, it is necessary to convert a list into a tuple or vice versa. First of all, we will explain how to convert a list to a tuple in Python 3, and in the following section you will know how to do it in reverse. So, follow these steps:

  1. Start by writing tuple(), then place the list inside the parentheses.
  2. Secondly, execute the code so that the conversion is made.
  3. And finally, verify if the list has been converted correctly to a tuple.

We show you a small example:

lista = [1, 2, 3]
tupla = tuple(lista)
print(tupla)

Here we have started by converting the list [1, 2, 3] into a tuple using the tuple() function. The resulting tuple is assigned to the tuple variable, and then printed on the screen using the print() function.

info When converting a list into a tuple, the elements of the list are preserved in the same order in the resulting tuple. Additionally, it is immutable, so it cannot be modified after its creation.

Tuple to List Conversion

To recap a bit, both lists and tuples are data types used to store collections of elements. However, unlike lists, tuples cannot be modified once created.

Therefore, if you need to make any modification to a collection of elements stored in a tuple, you have to convert it into a list. For this reason, we will now show you how to perform this conversion:

  1. First, write list(), and immediately place the tuple inside the parentheses.
  2. Then, execute the code, this way you can make this conversion.
  3. To finish, don't forget to verify if the tuple has been converted correctly to a list.

And here is an example to show it:

tupla = (1, 2, 3)
lista = list(tupla)
print(lista)

In this example, the tuple (1, 2, 3) is converted into a list using the list() function. The resulting list is assigned to the list variable, and then it is printed on the screen using the print() function.

info Remember that, when converting a tuple into a list, the elements of the tuple are preserved in the same order in the resulting list. Also, it can be modified after its creation.

List to String Conversion

The conversion of a list to a string can be useful when it is necessary to print or display the list as a string. To perform this conversion in Python, the join() method is used on an empty string, passing the list to be converted as an argument.

Below, we show you an example of how to convert a list of words into a string separated by commas:

palabras = ['Hello', 'world', 'from', 'Python']
cadena_palabras = ', '.join(palabras)
print(cadena_palabras)

First, the list palabras is defined, containing four elements. Then, the join() method is applied to an empty string with the list palabras as an argument. The result is assigned to the variable cadena_palabras. Finally, the string cadena_palabras is printed, containing the elements of the list separated by commas and a space.

info Keep in mind that the join() method only works with lists of elements of string type. If the list contains elements of other data types, they must be converted to strings before using the join() method.

String to List Conversion

The conversion of a string to a list is a common process in Python that allows converting a text string into a list of characters. This can be useful in situations where it is necessary to manipulate the characters individually or perform operations on the list.

In Python, this conversion can be done by applying the split() method. This method divides a string into substrings using a specific delimiter and returns a list of the resulting substrings.

To convert a string into a list in Python, follow these steps:

  1. Define a string.
  2. Call the split() method on the string and specify the desired delimiter.
  3. Store the result in a variable.

Here we leave you an example, which shows how you can make this conversion:

# Definir una cadena de caracteres
cadena = 'Hello, this is a string'

# Convertir la cadena de caracteres en una lista utilizando el método split()
lista = cadena.split(', ')

# Imprimir la lista resultante
print(lista)

This code will produce the following output:

['Hello', 'this is a string']

In this example, the string is divided into substrings using the comma and the space as delimiters, and then it is stored in the variable 'lista'. The resulting list contains two elements: 'Hello' and 'this is a string'.

success Done! With this tutorial, you can now convert different data types in Python. Remember that type conversion is a useful tool, but you must ensure that you are converting the data correctly to avoid errors in your code.