Python has many data types, and as a programmer, you should know how to convert one data type of Python into another. Python has many built-in functions for this task. And in this simple guide, we’ll be taking a look at them to help you understand type conversions better.
After you’re through with this guide, you would know how to convert strings into integers and vice versa. So without further ado, let’s get started.
Data Types in Python
Before we begin our discussion on type conversion in Python, let’s take a brief look at all data types present in this language:
Integer values can be as long as you want. Like in mathematics, integers in Python are digits too. But you can’t have indefinitely long integers unless you have a supercomputer. Python treats any number without a prefix as an integer. These numbers include decimal digits too.
Floating Point Numbers
Floats in Python are numbers that are written with a decimal point for separating the fractional digits and the integer. Some examples of floating-point numbers are 4.3, 9.18, etc. You can add ‘e’ for the scientific notation in Python as well.
Complex numbers in Python follow this format: (the real part + the imaginary part). Some examples of complex numbers are (3+2i), (8-2x), etc.
Strings can be any particular set of characters in Python. They are referred to as str. You can write them by using quotation marks. You can use single quotes as well as double quotes for writing strings in Python. Strings can be any characters placed in quotation marks such as ’91’, “hello”, etc.
Just like integers, there is no limit to character length in strings. Strings are of various types, such as triple-quoted strings, raw strings, and many others. However, if we would start discussing strings and their kinds, this article will go too long.
Boolean Data Type
The final data type we have left to discuss is the Boolean data type. Boolean data can have one of two values, which are true and false. Boolean values are quite essential in Python for many reasons. You can use Boolean values to determine the truthiness of objects and values.
As you’ll learn more about logical operators in Python, you will have to deal with Boolean objects and values.
So these were all data types present in Python. Learn more about the data types in Python. Apart from these data types, Python also has many built-in functions, which help it in performing a variety of tasks. We’ll need to use a few built-in functions to implement type conversion as well. Checkout our data science programs to learn about various types of conversions.
Now that we’ve discussed data types, we can move onto type conversion.
Different Kinds of Type Conversions in Python
There are two kinds of type conversions in Python. They are the following:
In this case, Python converts a data type into another type automatically. You, the user, don’t have to get involved in this process. Here is an example of implicit conversion of integer to float in Python:
num_int = 18
num_flo = 1.8
num_new = num_int + num_flo
print(“datatype of num_int:”,type(num_int))
print(“datatype of num_flo:”,type(num_flo))
print(“Value of num_new:”,num_new)
print(“datatype of num_new:”,type(num_new))
The output of the above code would be the following:
datatype of num_int: <class ‘int’>
datatype of num_flo: <class ‘float’>
Value of num_new: 19.8
datatype of num_new: <class ‘float’>
As you can see, we only had to add num_flo and num_int along with num_new to get the desired output. We converted the integer 18 into a float 19.8 through a few lines of code. The vital thing to note here is that Python turned the integer into float automatically.
In the case of explicit type conversion, we use functions for converting purposes. Remember the built-in functions we mentioned before? They will come in handy in this section. Another name for explicit type conversions is typecasting. It has this name because you, the user, cast the data type of the concerned object.
There are some type conversion functions you should be familiar with to perform typecasting in Python. Int(a,base) is a popular function as it can convert any data type into an integer. float() can turn any data type into a floating-point number. oct() converts integers to octal strings, and hex() converts integers to hexadecimal strings.
Here is an example of type conversion using oct():
c = oct(80)
print (“Output : “,end=””)
And the output would be:
Output : 0o120
When working with these functions, remember that the syntax for such conversion should be:
There are many kinds of type conversions you can perform in Python. However, there are two kinds of them, which you’ll be performing the most. Converting strings into integers and vice versa can be a great starting point. So let’s discuss each of those type conversions separately for a better understanding of this topic:
Converting String to int Python
You can convert a string to int Python through the int() function. It gives you a decimal integer with just a click. It assumes that the expression you’ve entered is a decimal integer, but if it’s not, it will return a ValueError. And we don’t want that. You can mention the number system you require by giving the value a base to avoid this problem.
So, if you’re converting a string to int in Python you can do this:
Or, you can add a base for different kinds of strings:
int(“5” , base=10)
Now let’s discuss the reverse of this conversion, i.e. when you need to convert an integer into a string.
Converting int to String Python
You can use the str() function for converting integers into strings. Just follow the syntax we mentioned before:
Strings can convert integers into decimal expressions if you use a binary literal. However, just like with the int function, if you require, you can add more information for different conversions. You can get octal, binary, and even hexadecimal results with this function.
After a little practice, you can perform more complex tasks with type conversion as well. Here’s an example of adding a string with an integer through typecasting:
num_int = 256
num_str = “64”
print(“Data type of num_int:”,type(num_int))
print(“Data type of num_str before Type Casting:”,type(num_str))
num_str = int(num_str)
print(“Data type of num_str after Type Casting:”,type(num_str))
num_sum = num_int + num_str
print(“Sum of num_int and num_str:”,num_sum)
print(“Data type of the sum:”,type(num_sum))
The output of the above code would be this:
Data type of num_int: <class ‘int’>
Data type of num_str before Type Casting: <class ‘str’>
Data type of num_str after Type Casting: <class ‘int’>
Sum of num_int and num_str: 320
Data type of the sum: <class ‘int’>
As you can see, it’s a lot of fun. Feel free to experiment with type conversions and expand your knowledge base.
We hope you learned a lot from this article. Performing type conversions is one of the many things you can do with Python’s functions.
If you are reading this article, most likely you have ambitions towards becoming a Python developer. If you’re interested to learn python & want to get your hands dirty on various tools and libraries, check out IIIT-B’s PG Diploma in Data Science.