Go to the Previous Tutorial on Learn Python From Scratch

Previous Python Tutorial

In the previous tutorial, we have learnt about the basic Data types in Python Programming. In this video, we will learn about the ‘Type Conversion’ in Python. Before getting started, let’s also look at how we denote each basic datatype in python.

Denoting Data Types in Python

As we learnt in the previous tutorial, we have some basic data types in Python like Integer, Float, Character and  String. These datatypes are to be denoted in a specific form, so that the Python Interpreter understands it. So, how do we denote these datatypes in Python Programming? Here you go

  • Integer – int
  • Float – float
  • Character – chr
  • String – str

Type Conversion

Type conversion is nothing but, the way of converting one data-type to another. For example let’s say we have a variable of Integer (int) data type, and we want to convert it into String data type of float data type, as our program demands, so we explicitly (meaning, manually) ‘type convert’ the data type of the variable.

Why should we use it?

At the end of the previous video tutorial, we discussed about the input() statement in Python. We learnt that, by default, input() function takes the value entered by the users during the program execution, in terms of string (str) . For the same reason, we got a weird result, when we added two numbers (which we inputted from the user using the input() function), that is, for example, when the two numbers inputted from the user are 5 and 3 respectively, we got a result 53 when we added the two numbers. The reason is, since by default 5 and 3 will be in the str datatype, they can’t be added numerically, but they can just be concatenated.

So, in this scenario we failed badly, because we want the sum of two numbers, but we are getting just the concatenated form of the two strings (5 and 3, for example). Therefore, in-order to actually get the sum of the two numbers, it is necessary to explicitly convert the inputted numbers into int datatype, which is what we termed as ‘type conversion’. In this scenario, you could see how important type conversion is. Remember that this is just one of a scenario, and there are many scenarios where type conversion plays an important role.

Converting str to int 

So, converting a String(str) to Integer(int), is a basic thing to do when we are dealing with input() statement, as explained earlier.
So, to explicitly convert a str into a int , enclose the variable or the int value inside the open and closed peranthasis of str()

The function int() also takes a second parameter base, but we will not be looking at it, in this tutorial.

Suppose, we want to convert a str value “236” into a int datatype
converted_int = int(“236”)
So, now the variable converted_int has the value 236 in int datatype.

Suppose we want to convert a str variable into int type.
a = “196”
converted_int = int(a)
Now, the variable converted_int contains the value of the variable in int datatype.

Similarly, lets say you inputted a number from the user using input() statement, and yiou want to convert it into int
a = input()
#inputting a number, which is by default stored as str
b = int(a) 
#Converting the inputted value from str to int.

Or, you can directly say like this,
a = int(input())

Remember, that you can convert from str to int, only if the value or variable to be converted to int is a number.
For example, if you say ,
converted_int = int(“Python”) 
Then, you will get a Value Error, because “Python” is not a number, and can’t be converted to int

 

Converting int to str

In the same way, you can do the vice-versa that is, convert int to str

Here is how you do it,
converted_str = str(12)
The value of converted_str will be “12”
However, there are no restrictions to convert a int to str

Converting float to str or vice-versa

In the same way, a float can also be converted to a str
Here is how you do it,
converted_str = str(12.6)
So, the value of converted_str will be the str form of the float 12.6  which is “12.6″

Vice-versa, str to float
converted_float = float(“56.0”)

Converting int to float or vice-versa

Same way, int and flat can also be inter-converted.

Convert an int to float like this,
converted_float = float(13)
The value of the variable converted_float will now be 13.0

Convert a float to int like this,
converted_int = int(16.9)
converted_int now will be 16

Converting chr to int and vice-versa

Now, it makes sense if we convert a numeric character (like ’15’ or 15.0′) into int. Intuitively thinking, any chr which is not numeric (like ‘a’ or ‘@’ or ‘ ‘) cannot be converted to int datatype. But you are wrong, we can actually convert any chr into int datatype.
To understand this, you need to understand about ASCII Codes.

What are ASCII Codes?

Computers can only understand numbers. So, whenever we press any key on our keyboard, to give something (a letter, number, or a string) as an input to the computer, it converts each character into a unique number known as ASCII Codes,
so that it can understand the input given by the user through the keyboard.
These ASCII values are unique for each character (case sensitivity is also taken into account) on the keyboard. The ASCII codes of some of the common used characters are listed below. TO READ THE COMPLETE LIST OF ASCII CODES, CLICK HERE

  • a to z – 98 to 127
  • A to Z – 65 to 90
  • 0 to 9 – 48 to 57

So, the ascii of ‘a’ is 98, which means whenever we type ‘a’ as an input, it is converted into the number 98 and is then understood by the computer.

Now, coming back to our old topic, converting chr to int and vice -versa, we can actually take a character, and find its ASCII value (which is in int datatype), and in that way we are actually converting chr to int datatype.
But, you can’t do it in the normal way which we followed for the rest of the cases. That is,
int(‘a’) will not work , and it gives a ValueError.

Instead, you must do the following,
converted_int = ord(‘a’)
which will make the converted_int = 98.  Note that, ord() is the function we use to get the ASCII of any character.

Convert int into chr like this,
converted_chr = chr(98)

This will assign the character ‘a’ to the variable converted_chr since the ascii of ‘a’ is 98. Note that there is no special function to convert int into chr (as in the case of converting chr to int), and we follow the same way as we did for others, that is, enclose the value to be converted inside the parentheses of the datatype name to which we want to convert the given value.

So, that is all for this tutorial, meet you in the next one!

Cheers!

 

 

 

Next Tutorial Coming Soon!

Share This

Share This

Share this post with your friends!

Subscribe To Our Newsletter and Never Miss an Update!

Subscribe To Our Newsletter and Never Miss an Update!

Leave us your Email ID and we will notify you whenever we release new Videos, Articles or Apps.

You have Successfully Subscribed!