Python Type-Casting

In this tutorial of Python Type-Casting and Type-Conversion, you’ll learn to transform variables in python through the using of built-in type-casting functions such as int(), str() etc.

Typecasting relates to the conversion of a variable of one primitive type to another primitive type. Most programming languages contain data-types such as integer, float, string, boolean also built-in keywords are available to convert them to other datatypes such as converting string type to integer type.

Post Update Timeline

Updated on: 01-07-2020

Added new topics such as converting datatypes into other datatypes, Implicit and Explicit Conversion and program to check variable datatype

Some examples of the basic level of datatype transformation and changing in Python.

Example

x="10"
y=5
z=int(x)+y

print(z)

#PYTHON OUTPUT
15

The variable x initial type was a string. But int() the function altered it into integer type.

Note

In the above example variable x is of type of string but it is converted to an integer variable using constructor int(variable_name)

Modifying variable string into float type

x="10.24"
y=5
z=float(x)+y

print(z)

#PYTHON OUTPUT
15.24

Example

x= 100
x=str(x)
print(x)
print(type(x))

#PYTHON OUTPUT
100
<class 'str'>

Note

In general programming, there are two types of conversions: Implicit and Explicit type conversion.

  • Implicit conversion: This means the programming language itself automatically converts the data-type with the need of a programmer to specify which type the variable must be converted to.
  • Example:
        
            num1 = 2
            num2 = True
    
            print(num1+num2)
    
            #PYTHON OUTPUT
            3
    

    In the above program, the variable num1 is of type integer and variable num2 is of boolean type. If you add both variables num1+num2 than the output of both numbers will be 3.

    And the main reason behind this is that 1 is the binary equivalent of true. So while adding python in background converts the boolean value of the variable num2 as 1.

  • Explicit conversion: means that programmer must manually specify the program to convert the variable to a particular datatype before performing any operation on or through it.
  • Example:
        
            num1 = 2
            num2 = "3"
            
            print(num1+int(num2))
    
            #PYTHON OUTPUT
            5
    

    In the above program, the variable num1 is of type integer and variable num2 is of string type.
    If you add both variables num1+num2 than TypeError: unsupported operand type error is displayed by if we use int(num2) and then add using num1+int(num2) we get the output as 5.

Progam to check variables Data-type

person = {
    "name" : "Suresh Kumar",
    "age" : 24,
    "is_student" : True,
    "marks_secured" : 257.50
}

if type(person['name']) == str:
    print("The name is string type.\n")

if type(person['age']) == int:
    print("The age is integer type.\n")

if type(person['is_student'] == bool:
    print("The is_student is boolean type.\n")

if type(person['marks_secured']) == float:
    print("The marks_secured is float type.\n")

#PYTHON OUTPUT
The name is string type.

The age is integer type.

The is_student is boolean type.

The marks_secured is float type.

Python Typecasting and Type conversion -program to check datatype of variable using type function

Conclusion

You have come to an end of the post on Python Type-Casting and Type-Conversion. For queries and suggestion comment on the below section and encourage us by sharing this post.

Related Posts

Summary
Review Date
Reviewed Item
Python Type-Casting
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Languages