close

How to Check Variable Type in Python

Hello Guys How Are You All? Hope You all are fine. Today in This tutorial We are going to learn About How to Check Variable Type in Python. So lets get start this tutorial without wasting your time.

How to Check Variable Type in Python

  1. How to Check Variable Type in Python

    To Check Variable Type in Python With the help of this method you can check the type of variable by using type() syntax : type(object)Input : num = 123 print(type(num)) num = 12.53 print(type(num)) num = “hello” print (type(num)) Output :<class 'int'> <class 'float'> <class 'str'>From this method you can know the type of variable very easily.

  2. Check Variable Type in Python

    To Check Variable Type in Python This method also use to check the type of variable in python.Syntax :isinstance(object , classtype)An object which is comparing with classtype. It will return true if the type matches otherwise it gives false. Input : age = isinstance(24,int) print("age is an integer:", age) weight isinstance(44.5,float) print("weight is a float:",weight) Output :age is an integer: True weight is a float: True
    As you can see if your data type is right then it will gives you true otherwise it gives you false. Thus you can check the type of variable in python from these methods.

  3. python check variable type

    To Check Variable Type in Python To Check Variable Type in Python This method also use to check the type of variable in python.Syntax :isinstance(object , classtype)An object which is comparing with classtype. It will return true if the type matches otherwise it gives false. Input : age = isinstance(24,int) print("age is an integer:", age) weight isinstance(44.5,float) print("weight is a float:",weight) Output :age is an integer: True weight is a float: True
    As you can see if your data type is right then it will gives you true otherwise it gives you false. Thus you can check the type of variable in python from these methods.

Variables and its Type in Python

1. Python String Variable

stringVariable = "This is a string"
print(stringVariable)

2. Python Boolean Variable

boolVariable = True
print(boolVariable)

3. Python Intiger Variable

intVariable = 50
print(intVariable)

4. Python Float Variable

floatVariable = 50.80
print(floatVariable)

Hope You Understand Variable and Its type. Now

Method 1: Use type()

With the help of this method you can check the type of variable by using type()

syntax : type(object)

Input :

num = 123
print(type(num))
num = 12.53
print(type(num))
num = "hello"
print (type(num))

Output :

<class 'int'>
<class 'float'>
<class 'str'>

From this method you can know the type of variable very easily.

Method 2: Use isinstance()

This method also use to check the type of variable in python.

Syntax :

isinstance(object , classtype)

An object which is comparing with classtype. It will return true if the type matches otherwise it gives false.

Input :

age = isinstance(24,int)
print("age is an integer:", age)
weight = isinstance(44.5,float)
print("weight is a float:",weight)

Output :

age is an integer: True
weight is a float: True

As you can see if your data type is right then it will gives you true otherwise it gives you false. Thus you can check the type of variable from these methods.

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment