Guides and tutorials

Hundreds of tutorials and step by step guides carefully written by our support team.

How to convert data types in Python 3

Introduction

Python is a high-level programming language that uses dynamic types. This means that you do not 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 types of data to create effective and efficient programs. In Python 3, data type conversion is a fundamental skill that every programmer should know.

Data type conversion is the process of changing one data type to another, and 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 program 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 Python Data Types

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 float in Python 3, you can use the float() function. You would need to follow these steps:

  1. First, type float(), then place the value you want to convert inside the parentheses.
  2. Next, execute the code to perform the conversion.
  3. Important, check if it has been converted correctly.

Here is an example code:

integer_number = 10
float_number = float(integer_number)
print(float_number)

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

It is important to add that, when converting an integer to a floating number, it is important to add a decimal point and a zero at the end of the number. This is because, in Python, a floating number must always have a decimal point to indicate that it is a number with decimal places.

info conversion is a simple operation in Python 3. It can be useful in different situations where you need to work with numeric data.

Float to integer conversion

In Python 3, you can convert a floating number to an integer 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. type int() and place the float value you want to convert inside the parentheses.
  2. Execute the code so you can do the conversion.
  3. Finally, check if the value has been converted correctly.

A code example:

float_number = 10.6
integer_number = int(float_number)
print(integer_number)

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

info Important to note, when converting a floating number to an integer, the decimal part of the number is lost, i.e. the digit is truncated. Therefore, it is possible to lose information if you are working with numbers that require decimals.

To round a number to the nearest integer, you can use the round() function, which shares the same functionality as int(). It is important to note that, when rounding a number with decimal .5, 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.

An example of code:

floating_number = 10.6
integer_number = round(float_number)
print(integer_number)

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

Integer to string conversion

Converting 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 screen. In Python 3, you can convert an integer to a string using the str() function. Here's how and an example:

  1. Start by typing str(), also, placing the numeric value you want to convert inside the parentheses.
  2. Then execute the code to do the conversion.
  3. Don't forget to check the value, so you know if it has been converted properly.

The code example:

example_number = 4
string_example = "Example number " + str(example_number)
print(string_example)

In this case, the integer 4 is converted to a string "4" using the str() function, appending it after the string "Example number ". In addition, the string is printed to the screen using the print() function.

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

Conversion from float to character string

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

  1. First of all, type str() and place the float value you want to convert inside the parentheses.
  2. Then, execute the code to perform this conversion.
  3. Finally, check if the conversion was successful.

And here is a code example for better understanding:

float_number = 3.14159
string_characters = str(float_number)
print(string_characters)

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

info Note that when converting a floating number into a string, information about its numeric nature is lost. Therefore, the output string can be used to display the number on screen or to manipulate it as a string, but not to perform numeric operations.

Character string to integer conversion

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

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

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

  1. When you are done typing and placing, execute the code to get the conversion.
  2. Finally, you need to check if the value has been converted correctly.

Here is an example:

string_characters = "123"
integer_number = int(string_characters)
print(integer_number)

Here the string "123" is converted to an integer using the int() function. The resulting number is then printed to the screen using the print() function.

info It should be noted that, when converting a string of characters to an integer, any additional information that may be present in the string may be lost. So, if the string contains non-numeric characters or additional information, the conversion will not be possible.

Character string to float conversion

As with converting a string to an integer, it is possible to convert a string containing a number with decimal places to a numeric value of type float in Python 3. To perform this conversion, you can use the float() function and follow these steps:

  1. Start by typing float() and place the string containing the number with decimals inside the brackets.

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

  1. Next, execute the code to perform the conversion.
  2. Finally, check if it has been converted correctly.

Also, here is a code example:

character_string = "3.14".
float_number = float(string_characters)
print(float_number)

In this case, the string "3.14" is converted to a float number using the float() function. The resulting number is then printed to the screen using the print() function.

info Note that, when converting a string to a floating number, any additional information that may be in the string is lost. So, if the string contains non-numeric characters or additional information, the conversion cannot be done.

So far you have seen basic data conversions in Python. Now we'll show you how to do more basic data conversions.

Complex data types in Python

List to tuple conversion

In Python, both lists and tuples are data types used to store collections of items. Lists are mutable, meaning that you can add, remove, or modify elements. In contrast, tuples are immutable, meaning that they cannot be modified after they are created. Sometimes, 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 next section you will know how to convert a list to a tuple in Python 3. So, follow these steps:

  1. Start by typing tuple(), then place the list inside the parentheses.
  2. Secondly, execute the code so that the conversion is done.
  3. And finally, check if the list has been successfully converted to a tuple.

Here is a small example:

list = [1, 2, 3]
tuple = tuple(list)
print(tuple)

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 variable tuple, which is then printed to the screen using the print() function.

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

Tuple to list conversion

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

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

  1. First, type list(), and then place the tuple inside the parentheses.
  2. Then, execute the code, so you can do this conversion.
  3. Finally, don't forget to check if the tuple has been converted correctly to a list.

And here is an example to show it:

tuple = (1, 2, 3)
list = list(tuple)
print(list)

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, then printed to 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

Converting a list to a string can be useful when you need to print or display the list as a string. To perform this conversion in Python, you use the join() method on an empty string, passing the list you want to convert as an argument.

Here is an example of how to convert a list of words into a comma-separated string:

words = ['Hello', 'world', 'since', 'Python']
string_words = ', '.join(words)
print(string_words)

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

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

Character string to list conversion

String to list conversion is a common Python process that allows you to convert a string of text into a list of characters. This can be useful in situations where you need to manipulate the characters individually or perform operations on the list.

In Python, you can do this conversion by applying the split() method. This method splits 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 is an example, which shows how you can do this conversion:

# Define a string
string = "Hello, this is a string".

# Convert the string to a list using the split() method
list = string.split(", ")

# Print the resulting list
print(list)

This code will produce the following output:

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

In this example, the string is split into substrings using comma and space delimiters, and then stored in the variable "list". The resulting list contains two elements: "Hello" and "this is a character string".

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