More about Type Casting in Python

Now let’s study type casting in Python. Before knowing this you must aware of word casting so casting is the simple concept that to convert one data type to another okay. So as Python is an object oriented programming language in which data types are represented are as classes.

To do type casting you have to use constructor functions that are present in python okay we see some of them as example see below:

int() Constructor in Python

This type of constructor construct integer value from different inputs see below example

x = int(10)
y = int(2.88)
z = int("33")
print(x)
print(y)
print(z)

Output

10
2
33

NOTE: we have gave various inputs to int() constructor but it gives us integer value you can aslo check their data type for verification see below.

x = int(10)
y = int(2.88)
z = int("33")
print(type(x))
print(type(y))
print(type(z))

Output

<class 'int'>
<class 'int'>
<class 'int'>

See it’s give us all that these are all integer types okay. Now check another constructor.

float() Constructor in Python

x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
print(x)
print(y)
print(z)
print(w)

Output

1.0
2.8
3.0
4.2

NOTE: we have gave various inputs to float() constructor but it gives us floating numbers value only you can aslo check their data type of all variables for verification see below.

x = float(1)
y = float(2.8)
z = float("3")
w = float("4.2")
print(type(x))
print(type(y))
print(type(z))
print(type(w))

Output

<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>

See it’s give us all that these are all float types okay. Now you can check for string also like that above okay.

Link: https://Codelikechamp.com

Medium Link: Follow me on Medium

Linkedin Link: Follow me on Linkedin

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

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

Scroll to Top