How To Print Type Of Variable In Python — pythonpip.com

Parvez Alam
2 min readApr 16, 2022

The type() method in Python can be used to print the type of a variable. type() is a Python built-in function that returns the variable’s data type. In Python, use the print() method to print the variables.

How To Print type of variable

The type() method in Python can be used to determine a variable’s data type. The type() method returns the argument(object) supplied as a parameter’s class type. It will return output with the class <’list’> if the input is a list,<’string’> if the input is a string, and so on.

Parameters

A single object is passed to type().

Example :

Let’s print the different types of variables.

str = "Welcome to pythonpip" age = 34 salary = 7342.7 complex_num = 11j+21 a_list = ["name", "age", "salary"] emps = ("A", "B", "C") a_dict = {"A": "a", "B": "b", "C": "c"} print("The type is : ", type(str)) print("The type is : ", type(age)) print("The type is : ", type(salary)) print("The type is : ", type(complex_num)) print("The type is : ", type(a_list)) print("The type is : ", type(emps)) print("The type is : ", type(a_dict))

Output:

The type is : <class 'str'> The type is : <class 'int'> The type is : <class 'float'> The type is : <class 'complex'> The type is : <class 'list'> The type is : <class 'tuple'> The type is : <class 'dict'> ** Process exited - Return Code: 0 ** Press Enter to exit terminal

Python isinstance

The isinstance() is a built-in Python method that returns True if a specified object is of the specified type. The isinstance() method accepts two arguments: object and classtype and returns True if the defined object is of the defined type.

Syntax :

isinstance(object, classtype)

Arguments:

object: You’re comparing the instance of an object to the class type. If the type matches, it will return True; otherwise, it will return False.

class type: It is a type or a class or a tuple of types and classes.

Example

Let’s compare the float value with type float, i.e., 13.15 value will be compared with type float.

fvalue = 13.15 inst_t = isinstance(fvalue, float) print("Instance Type:", inst_t)

Output :
Instance Type: True

The above code returns True value.

Originally published at https://www.pythonpip.com on April 16, 2022.

--

--

Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I love learning and sharing knowledge. https://www.phpflow.com/