How to Get and Print Type of Variable in Python

Here are two ways to print a type of variable in Python:

  1. Using type() 
  2. Using isinstance()

Method 1: Using type()

The type() function returns the type of the object passed to it.

This function is a very useful especially when you are dealing with dynamic typing or if you’re unsure of a variable’s type.

Visual Representation

Visual representation of using the type()

Example

# Define various types of variables
str = "Matthew Perry"
died = True
day = 28
complex_num = 28j+23
num = 3.14
my_list = [1, 3, 5, 7, 9]
my_tuple = (2, 4, 6, 8, 10)
my_set = {5, 10, 15, 20, 25}
my_dict = {'a': 11, 'b': 22,'c': 33, 'd': 44}

# Printing the type of each variable
print("The type is : ", type(str))
print("The type is : ", type(died))
print("The type is : ", type(day))
print("The type is : ", type(complex_num))
print("The type is : ", type(num))
print("The type is : ", type(my_list))
print("The type is : ", type(my_tuple))
print("The type is : ", type(my_set))
print("The type is : ", type(my_dict))

Output

The type is : <class 'str'>
The type is : <class 'bool'>
The type is : <class 'int'>
The type is : <class 'complex'>
The type is : <class 'float'>
The type is : <class 'list'>
The type is : <class 'tuple'>
The type is : <class 'set'>
The type is : <class 'dict'>

Method 2: Using isinstance()

The isinstance() function is used to check if an object is an instance of a specific class.

This approach is more manual compared to using type(), but it allows for custom handling or messages for each type.

Example

# Define various types of variables
my_str = "Matthew Perry"
died = True
day = 28
complex_num = 28j+23
num = 3.14
my_list = [1, 3, 5, 7, 9]
my_tuple = (2, 4, 6, 8, 10)
my_set = {5, 10, 15, 20, 25}
my_dict = {'a': 11, 'b': 22,'c': 33, 'd': 44}

# Function to print the type of a variable using isinstance
def print_type(variable):
 if isinstance(variable, str):
 print(f"The variable is of type str")
 elif isinstance(variable, bool):
 print(f"The variable is of type bool")
 elif isinstance(variable, int):
 print(f"The variable is of type int")
 elif isinstance(variable, complex):
 print(f"The variable is of type complex")
 elif isinstance(variable, float):
 print(f"The variable is of type float") 
 elif isinstance(variable, list):
 print(f"The variable is of type list")
 elif isinstance(variable, tuple):
 print(f"The variable is of type tuple")
 elif isinstance(variable, set):
 print(f"The variable is of type set")
 elif isinstance(variable, dict):
 print(f"The variable is of type dict")
 else:
 print("Unknown type")

# Using the function to print the type of each variable
print_type(my_str)
print_type(died)
print_type(day)
print_type(complex_num)
print_type(num)
print_type(my_list)
print_type(my_tuple)
print_type(my_set)
print_type(my_dict)

Output

The variable is of type str
The variable is of type bool
The variable is of type int
The variable is of type complex
The variable is of type float
The variable is of type list
The variable is of type tuple
The variable is of type set
The variable is of type dict

Related posts

Printing to stderr

Printing bold text

Printing an array

3 thoughts on “How to Get and Print Type of Variable in Python”

  1. Hi,
    I just started to learn Python.
    I have variables:
    x=1j
    y=2.2
    Now, I would like to print them in a single print statement on two-line output, to show something like this:
    X = 1j , , with comment1
    Y = 2.3, , with comment2
    Your advice would be greatly appreciated.
    Cheers,
    Zoltan.

    Reply
  2. In the printout I would like to show the type() of the given variable as well. (I wrote it but
    only two commas appeared instead.)

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.