In this tutorial we are going to learn how to check variable type in Python.
One of the features of Python is it uses Dynamic data type. It means we need not to specify which type of data the variable is going to hold. One time in our code a variable can hold integer later it may hold some string and this will not give us any error.
Check out the code below for explanation:
x = 2 print(x) # x is holding int x = "I am string" print(x) # x is holding string
Output
2
I am string
So now the need arises to know the data type of any variable which will be useful in many cases, the same operation may have different effects on different data types.
For example + operator is used for addition in case of int and concatenation in case of string.
x = 16 y = 8 print(x+y) x = "16" y = "8"
Output:
24
168
So now the important question is how can we know the data type of variable.
Python Check Variable Type
Python has provided three ways to do this.
- type()
- isinstance()
- __class__
Using type()
type() will only take one argument which will be the name of the variable and it will give us information about the type of the variable.
x = 16 print(type(x)) x = "16" print(type(x)) x = [[1,2],4,6] print(type(x)) x = 16.10 print(type(x))
Output:
<class ‘int’>
<class ‘str’>
<class ‘list’>
<class ‘float’>
x = 15L print(type(x))
This will give output long for Python version before 3.0 and error for later as the distinction between long and int goes away after Python 3.0
Before Python 3.0:
<type ‘long’>
After Python 3.0:
x = 15L
^
SyntaxError: invalid syntax
Use of isinstance()
Use of isinstance is bit different than type(). Isinstance take argument variable name and a type and if the variable name is same as type then it returns True otherwise it returns False.
x = 16 print(isinstance(x,int)) x = "16" print(isinstance(x,str)) x = [[1,2],4,6] print(isinstance(x,int)) x = 16.10 print(isinstance(x,float))
Output:
True
True
False
True
isinstance can be used when we are going to perform an operation on according to the type of the variable using if conditions.
We can also provide a Python tuple as second argument then if the type of the variable is one from the tuple then the method will return True else it will return False.
x = 16 print(isinstance(x,(int,str,list))) y = "16" print(isinstance(y,(int,dict,list)))
Output:
True
False
This can come in handy when we can perform the same operation on different data types. For example we can perform addition on int and similarly on float also so we can use the Python tuple can perform the same operation in one line instead of writing two if else conditions.
Using __class__
We can also use __class__ to know the variable type. Variable_name.__class__ will give us the class of that variable, the output here will be similar as if we are using type().
x = 16 print(x.__class__) x = "16" print(x.__class__) x = [[1,2],4,6] print(x.__class__) x = 16.10 print(x.__class__)
Output:
<class ‘int’>
<class ‘str’>
<class ‘list’>
<class ‘float’>
Although this will give us correct output similar to using type() method, it is not advised to use __class__ as names that start with __ in python are not part of public API so its best if we don’t use them.
Comment down below if you have any queries or know any other way to check variable type in python.