Student at BIT Mesra Aug. 4, 2020, 6:19 p.m. ⋅ 346 views

Type casting in Python

There might be times when we want a variable to be certain type. It usually happens when we need to assign value of one variable to another variable and both are not of same type.

To cast type of any variable in python we can use constructor functions of type of our choice.

  • int() - This gives integer type value after casting the value we pass to it. If we give it a float type variable it will round it down to previous integer value.
>>> int(5.8)

If we give it a string value, it will cast to integer value and give value equivalent to its whole number representation. 

>>> int("132")
  •  float() - This gives a float type variable of whatever we pass to it.
>>> float(213)
>>> float("13")
>>> float("14.9")
  • str()  - It returns a string type variable from several types of variables.
>>> str("Hello HackersFriend")
'Hello HackersFriend'
>>> str(12321)
>>> str(24.9)


Go back to feed

HackersFriend Updates

View more


Nov. 28, 2018, 5:30 p.m.

Python from zero to hero

place Delhi

View details

Aug. 13, 2018, 5:30 p.m.

Python from zero to hero

place Bangalore ( HackersFriend office BTM Layout)

View details