Python is a built-in method designed to test any object thanks to its subclasses. A Python beginner will find it hard to get familiar with various data types.
Yet, you can employ various Python functions to distinguish and obtain the variable type. How to check the type of variable in Python? Here is the detailed answer!
Here is the sample code to check the type of variable in Python
website = "Tutopal"
category = ["Python", "Java", "Javascript", "C#", "C++"]
article = {
"Subject": "How to check the type of variable in python",
"keyword": "check the type of variable",
"words": 500
}
traffic = 1000000
#Solution 1: Use isinstance() to check the type of variable in python
if isinstance(website, str) == True:
print('Type of website variable is string')
else:
print('Type of website variable is not string')
#Solution 2: Use type() to check the type of variable in python
print("Type of website variable: ", type(website))
print("Type of category variable: ", type(category))
print("Type of article variable: ", type(article))
print("Type of traffic variable: ", type(traffic))
Output:
Type of website variable is string
Type of website variable: <class 'str'>
Type of category variable: <class 'list'>
Type of article variable: <class 'dict'>
Type of traffic variable: <class 'int'>
Check The Type Of Variable In Python
There are two common functions to check the variable type in Python: isinstance() and type().
Method 1: Insinstance()
Using the isinstance()
function lets you check the variable type by taking inputting and returning the result in True/False answer. Here is the detailed code:
Sytnax
isinstance(object, type)
str_var = "Type is string"
if isinstance(str_var, str) == True:
print('Type of variable is string')
else:
print('Type of variable is not string')
Output:
Type of variable is string
Method 2: Use The Type() Built-in Function
Type() is a built Python function used to print the type of value stored in a variable. Although it is often useful for debugging purposes, it can also be exploited in returning the object’s type. Here is how this function works in action:
Code:
website = "Tutopal"
category = ["Python", "Java", "Javascript", "C#", "C++"]
article = {
"Subject": "How to check the type of variable in python",
"keyword": "check the type of variable",
"words": 500
}
traffic = 1000000
print("Type of website variable: ", type(website))
print("Type of category variable: ", type(category))
print("Type of article variable: ", type(article))
print("Type of traffic variable: ", type(traffic))
Output
Type of website variable: <class 'str'>
Type of category variable: <class 'list'>
Type of article variable: <class 'dict'>
Type of traffic variable: <class 'int'>
Why Shouldn’t Use _Class_ To Check The Data Type?
Names with underscore starting letters are often not a part of a public API in Python. Therefore, it would be best to avoid using them, including _class_ function.
For example:
class Website(object):
def website(self):
print('Name of Class: ', self.__class__)
f = Website()
f.website()
Output:
Name of Class: <class '__main__.Website'>
Conclusion
It may be confusing for beginners to check the type of variable. So how to check the type of variable in Python? You can use the two functions mentioned above to do this practice. Follow instructions and get the desired result.