How to Check Variable Type in Python

In Python, we use variables to store any type of data and perform any operations on it. All the variables have reserved memory locations, and the memory is consumed when we assign a value to a variable. The data type of variables tells us about the type of value we are storing in the variable. There are many types of data that are stored in variables like string, numerical, boolean, and image.

This article will explore the below-listed aspects to check variable type in Python. This post serves the following outcomes:

  • Using the “type()” Function
  • Using the “isinstance()” Method

How to Check Different Variable Types in Python?

In Python, it is not compulsory to define the data type because Python follows the rule of dynamic data type in which if we do not enter the type of data, it will automatically judge the data type and performs the function. We use the print() function to print the value stored in variables. Let’s head over to the methods check variable type in Python:

Method 1: Using the type() Function

The “type()” function is used to check the type of input variable in Python. we input the variable into the “type()” function parameter, and it returns the variable type. The syntax of the “type” function is provided below:

Syntax

type(<Variable_Name>)

Now let’s understand it with an example.

Code

a = 92
print(type(a))

b = 'Hello Guys'
print(type(b))

c = 0.998
print(type(c))

d = True
print(type(d))

e = [1,2,3,4]
print(type(e))

f = (9,8,7,6) 
print(type(f))

In the above code:

  • We assigned different variables of various data types.
  • The type() function is used as a parameter inside the print() function to print the variable type.

Output

In the above output, we have checked all data types of different variables we used in our code.

Method 2: Using the isinstance() Method.

The “isinstance()” function is used to check the variable types in python. The syntax of this “isinstance ()” method is given below:

Syntax

isinstance(object, classtype)

The “isinstance()” function has two parameters. The first parameter shows the value of the data type we want to check, and the second parameter returns its variable type. If the variable type is the same, then the “isinstance” method will return “True” or “False”.

Let’s understand the working of “isinstance()” function with an example:

Code

x = isinstance(91,int)
print('x is an integer :', x)

y = isinstance(3.14,float)
print('y is an float :', y)

z = isinstance([1,2,3,4,5,6],list)
print('z is an list :', z)

t = isinstance(9.1,int)
print('x is an integer :', t)

In the above code:

  • We have checked different data type values like “integer”, “float” and “list
  • The “isinstance()” function is used four times. The first parameter represents the value, and the second denotes the data type. If the value belongs to the respective data type, the output will be “True” else, “False”.

Output

The output value will return true if the type is matched with the input value and false when they do not match with each other.

Conclusion

In Python, the “type()” and “isinstance()” functions are used to check the variable type. The “type()” function takes the variable and returns its data type. On the other hand, the “isinstance()” function takes two parameters, the first denotes the value, and the second represents the datatype to be checked. If the value belongs to the specified datatype, it returns “true”; else, it throws “false”. This post has demonstrated numerous methods to check variable types in Python.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×