Casting in Python [Explained]

Casting in Python is done using functions like this:

  • int() – convert Data type to integer
  • float() – convert Data type to float
  • str() – convert Data type to String


Int():

a = int(2.4)
b = int("8")

print (a)
print (type(a))

print (b)
print (type(b))

Output:

2
<class 'int'>

8
<class 'int'>



Float():

a = float(2)
b = int("8")

print (a)
print (type(a))

print (b)
print (type(b))

Output:

2.0
<class 'float'>

8
<class 'int'>



str():

a = str(2.4)
b = str(8)

print (a)
print (type(a))

print (b)
print (type(b))

Output:

2.4
<class 'str'>

8
<class 'str'>

Bilel-A

Enthusiastic Network Engineer specializing in Cisco ACI, passionate about solving challenges. A lifelong learner who loves gaining and sharing knowledge. Profile: https://www.linkedin.com/in/bilel-ameur-71116b2b5
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x