Print Type of Variable in Python

Python print type of variable

What is the type of variable in Python?

We use variables to store data in a reserved memory location. The type of a variable defines the memory allocated to it. The type of a variable is based on the data we store in it. We do not need to explicitly mention the type of a variable while declaring it in Python. Some of the data types in Python are int, float, str, and more.

Ways to print type of variable in Python

In this tutorial, we will print type of variable in Python using different methods.

Using the type() function

We can use the type() function to return the data type of an object in Python. It is a built-in function.

For example,

Output:

In the above example,

  • We declare a variable a that contains a string and another variable b that stores a floating value.
  • The type() function tells the data type of these variables.
  • We display these values using the print() function.

Using the isinstance() function

We use the isinstance() function to check whether a variable belongs to a given data type or not. We pass the variable and the data type within the function, and it will return True if the type of the variable matches the given data type.

See the code below.

Output:

True
False

In the above example,

  • The variable a is a string, so the isinstance(a,str) returns True.
  • The variable b is a float value, so the isinstance(b,int) returns False.

Using the __class__() function

Functions that begin with an underscore are called magic functions in Python. It is not advisable to invoke such functions directly in Python.

The __class__ property returns the type or class of the given object.

For example,

Output:

Conclusion

In this tutorial, we discussed several methods to print the type of a variable in Python. The type(), isinstance(), and __Class__ methods return the type of a variable and we display it using the print() function.

That’s all about how to print type of variable in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63db6d8f67128717101801/] [crayon-63db6d8f6712f173442824/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63db6d8f6733f152911318/] [crayon-63db6d8f67345430247966/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63db6d8f674db626905612/] [crayon-63db6d8f674de776385719/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.