In programming, you need to store value in a variable to use it in your program. In other words, the variable is a placeholder for the data.
Data can be of different types like numerical value, string, image, etc.
So before using any variable in Python, you have to declare the variable in your code.
Let’s use this tutorial to learn- how to declare a variable. How to check the data type of each variable in Python?
Table of Contents
Python follows dynamic data types. This is one of the features of Python programming.
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.
Variable declaration in Python:
var = 27 #var is integer variable var = "computer" #var is string variable
The data type of the variable is decided based on the type of value assigned to the variable.
Later, I will share two programming methods in Python to check the type of variable.
You can use the type()
inbuilt function.
Pass the name of the variable name as input the method type(). It returns the data type of the variable.
For example:
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>
When you assign 10 to the variable, the variable becomes an integer type. Here, int and float are the numerical data types in Python.
Every data type has its significance. There are some supported functions for each data type.
Let’s say, we can not do arithmetic operations on string datatype. The concatenation of two objects is valid only for the string.
Sometimes, you may want to manipulate the data based on its data type. So comparing data types in Python is essential.
For example:
I am performing ‘+’ operations on integers as well as on string data types.
strIn1 = "20" strIn2 = "35" print('String output: ',strIn1 + strIn2) val1 = 20 val2 = 35 print('Numeric output: ',val1+val2)
Output:
String output: 2035 Numeric output: 55
In the above program,
So comparing data types in Python is essential before performing some operations.
The isinstance()
function takes two parameters as input.
If the variable (first parameter) is an instance of data type (mentioned as the second parameter), this function returns true.
For example:
avl =16 isinstance(val, int) #True isinstance(val, long) #False isinstance(val, str) #False
Here you can also pass the Python tuple as the second argument. The below code checks, if the variable ‘n’ belongs to any of the str, int, or float data type.
val = 17 isinstance(val, (str, int, float) #True
Here is a simple program that takes input from the user and saves it in a variable. Based on the data entered by the user, the 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.
Write a Python program to check if a variable is an integer or a string using isinstance()?
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")
type()
function:We saw earlier identifying the type of the variable using the type()
function. You can also use it to check the type of variable using the type()
method and perform an action based on its type.
Write a Python program to check if a variable is an integer or a string using 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!")
The above program checks the list and string type. You can use all other data types in the conditional operator.
You can use the above two methods to identify the object of the particular class.
1) Using type
function:
class MyFirstClass: var = 10 obj = MyFirstClass() type(obj) # __main__.MyFirstClass
2)Using isinstance()
function:
class MyFirstClass: var = 10 obj = MyFirstClass() isinstance(obj, MyFirstClass) #True
Wrapping up…
The above two methods isinstance()
and type()
to identify and check the data type of the variable, I find it very convenient to use in my code.
This is all from this tutorial for Python to check the type of variable. If you have any queries or points to discuss, comment below.
Happy Pythoning!
Program does not run
Hi Somnath,
Can you please mention the Python version you are using and the type of error you are getting?
As per corrected by Binu David, you are missing “)” in second elif statement of your code.
for python 3.8.3
try this program.
It appears that you are missing a “)” in your elif statement.
Thanks Binu for the correcting.
Edited!
Why you still use Python 2?
Till the last month, I was focus to maintain code for both Python 2 and Python 3. Now onwards, only Python 3.
What input I give, the output is “var is str”
Whatever input you give, input() always takes it as a string. Irrespective of input, the output will be “var is str”.
Ok… Thank you Very Much…
You’re welcome!
why you used “” for string values?
You can use either ” or “” for Python string.
output=enter the value 45
output=var is string
but if independently asked it is giving as:
n=45
isinstance(n,str)
False
Hi Vishal, when you take a user input using
input()
function, it returns string always.If you want to take an integer as an input from the user, you have to explicitly convert it using
int()
function.For Example,
This works only for the integer value. But, when I input the float value it will give an error.
For float value, you can use float() instead of int().
isinstance(n, (str, int, float) #True
seems as if it should be
isinstance(n, (str, int, float)) #True
Thanks for the correction. We missed the closing bracket.