How to Program Python to check type of variable? | float or int or string or class
You might be knowing Python follows dynamic data types. This means we don’t need to specify the datatype of the variable explicitly.
For example, here is Python code for declaring variables of a different type.
var = 27 #var is integer variable var = "computer" #var is string variable
The datatype of the variable is decided based on the type of value storing in the variable.
In this tutorial, I will share two methods for programming in Python to check type of variable?
But, how to know the datatype of the variable?
You can use the
type() inbuilt function. Pass the variable name to the function. It returns the datatype of the variable.
var = 10 type(var) #<type 'int'> var = 10.5 type(var) #<type 'float'> var = "Computer" type(var) #<type 'str'> var = [34, 57, 37] type(var) #<type 'list>
Why do you need to know the data type of variable?
Every data type has its own significance. There are some supported functions for each data type. Or you may want to manipulate the data based on its data type. So the comparing data types in Python is essential.
Let’s say, we can not do arithmetic operations on string datatype. Concatenation of two objects is valid only for the string.
So in many cases, the comparing data types in Python is essential before performing some operations.
Python to check type of variable:
Method 1: Using isinstance() function:
isinstance() function takes two parameters as an input.
If the variable (first parameter) is an instance of data type (mentioned as the second parameter), this function returns true.
n =16 isinstance(n, int) #True isinstance(n, long) #False isinstance(n, str) #False
Here you can also pass the tuple as the second argument. Below code checks, if the variable ‘n’ belongs to any of the str, int or float data type.
n =17 isinstance(n, (str, int, float) #True
Here is a simple program that takes input from the user and saves in variable var. Based on the data entered by the user, data type of the var changes.
If you are taking a number as an input, you can check if the number is int, float or long.
var = input("Enter the value: ") if(isinstance(var, float)): print "var is float." elif(isinstance(var, int)): print "var is integer." elif(isinstance(var,long): print "var is loog." else print "Unknow data type"
Method 2: Using
We saw earlier identifying the type of the variable using the
type() function. You can also make a use of it to check the type of variable and perform an action based on its type.
var = input("Enter the value: ") if type(var).__name__ == 'str': print "var is a string!" elif type(var).__name__ == 'list': print "var is a list!"
This above program is just to check the list and string type. You can use all other data types in the conditional operator.
Python Code to check if variable is specific class:
You can use above two methods to identify the object of the particular class.
class MyFirstClass: var = 10 obj = MyFirstClass() type(obj) # __main__.MyFirstClass
class MyFirstClass: var = 10 obj = MyFirstClass() isinstance(obj, MyFirstClass) #True
Above two methods, I find convenient to use to identify and check the datatype of the variable.
This is all from this tutorial for python to check type of variable. You can find more Python tricks in our Python tutorial.
If you have any query or point to discuss, comment below.
Keep Learning Python!