Multiple ways to get variable types in Python

This tutorial explains how to get variable type in python 2. x and Python 3. x. Sometimes, We need to check given variable type is a string.

Since Python is a dynamic programming language and variable types can be changed at runtime, It is very useful to print the variable type.

Check variable type in Python example

There are multiple ways we can check

  • using type() function type() function used to find the type of variable. It prints the variable data type

Syntax:

type(variable)

type is a keyword the variable is a python variable declared with an Inbuilt type

It returns the type of class name data.

Here is a type of python example

In the below examples,

  • Declared a variable of Inbuilt type and assigned with values
  • Python infers the type from the initialized value
  • To print the variable data type, used type with the variable name
  • To print the type,
    • use print type(number) in python 2.x
    • use print (type(number)) in python 3.x
number = 1
print (type(number)) #<class 'int'>

rate = 12.10
print (type(rate)) #<class 'float'>

complexValue = 1j
print (type(complexValue)) #<class 'complex'>

flag = False
print (type(flag)) #<class 'bool'>

str = "str"
print (type(str)) #<class 'str'>

array = [1, 2, 3, 4]
print (type(array)) # <class 'list'>



words =  ["one", "two", "three", "four", "five"]
print (type(words)) # <class 'list'>

user = {
    "id": 1,
    "username": "john",
    "Salary": 5000
}
print (type(user)) # <class 'dict'>

tupleType = ("string", 1, 12.11)
print (type(tupleType)) # <class 'tuple'>

setType = {"ten", "nine", "eight"}
print (type(setType)) # <class 'set'>

rangeType = range(6)
print (type(rangeType)) # <class 'range'>

value=b"one"
print (type(value)) # <class 'bytes'>

value1=None
print (type(value1)) # <class 'NoneType'>

value2=bytearray(15)
print (type(value2)) # <class 'bytearray'>

value3=memoryview(bytes(5))
print (type(value3)) # <class 'memoryview'>

Output:

<class 'int'>
<class 'float'>
<class 'complex'>
<class 'bool'>
<class 'str'>
<class 'list'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
<class 'set'>
<class 'range'>
<class 'bytes'>
<class 'NoneType'>
<class 'byte array'>
<class 'memoryview'>
  • using isinstance function isinstance checks the variable with the given type and returns abool value.

Syntax

isinstance(variable, type)

if variable is of type, return True, else False

Here is an example

number = 1
print (isinstance(number,int)) #true

rate = 12.10
print (isinstance(rate,float)) #true
complexValue = 1j
print (isinstance(complexValue,complex)) #true
flag = False
print (isinstance(flag,bool)) #true
str1 = "str"
print (isinstance(str1,str)) #true

arry = [1, 2, 3, 4]
print (isinstance(arry,list)) #true



words =  ["one", "two", "three", "four","five"]
print (isinstance(words,list)) #true

user = {
    "id": 1,
    "username": "john",
    "salary": 5000
}
print (isinstance(user,dict)) #true

tupleisinstance = ("string", 1, 12.11)
print (isinstance(tupleisinstance,tuple)) #true

setisinstance = {"ten", "nine", "eight"}
print (isinstance(setisinstance,set)) #true
rangeisinstance = range(6)
print (isinstance(rangeisinstance,range)) #true

value=b"one"
print (isinstance(value,bytes)) #true


value2=bytearray(15)
print (isinstance(value2,bytearray)) #true
value3=memoryview(bytes(5))
print (isinstance(value3,memoryview))#true

It is very useful to use in a conditional expression such as if-else.

How to check if the type of a variable is the string?

To check variable type is a string, Please follow the steps

  • Declared string variable for testing
  • used isinstance function to check variable type is an instance of a string and returns Bool value
  • used if the else conditional expression
name = "john"
print (isinstance(name,str)) #true
if isinstance(name,str):
print("variable is a string")
else:
print("variable is not a a string")

Output:

exist 1
not

Conclusion

Learned multiple ways to know the variable type in Python with examples

  • type() function
  • isinstance() function