Python type casting or conversion

Type casting or conversion is a process that allows changing a variable data type to another data type. For instance, money amount basically used floating point data type (100.00) but when users give input they most of the time give integer (100) value. This type of case needs to type conversion or casting, here integer value (100) needs to convert into a floating point (100.00).


Objectives

  • Type casting or conversion examples


Python type casting or conversion examples

Code

# String to Integer
integer_from_string = int("100")
print(integer_from_string)

# String to Float
float_from_string = float("100")
print(float_from_string)

# Integer to Float
integer_from_float = int(10.20)
print(integer_from_float)

# Integer to Float
float_from_integer = float(10)
print(float_from_integer)


# Float to String
string_from_float = str(10.30)
print(string_from_float)

Output

100
100.0
10
10.0
10.3

Process finished with exit code 0


Python type casting or conversion video tutorial