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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.