• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Python Print Variable Type Uses

Python Print Variable Type Uses

August 29, 2022 Leave a Comment

To print the variable type in Python, you can use the type() function to get the type and print it with print(). 

a = 1
b = "string"
c = [0, 1, 2]
d = (0, 1, 2)
e = {"key":"value"}
f = 1.0
g = {"set"}

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))

#Output:
<class 'int'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'float'>
<class 'set'>

When working with different objects in Python, the ability to check the type of your variables and objects easily is very important.

In Python, you can easily get the variable type and print it.

What Types Are in Python for Print Variable Type?

Python is a dynamically typed language, which means that the type is interpreted at runtime. Variables in Python can belong to various data types. Some common variable types in Python include:

  1. Integers (int): Whole numbers without a decimal point.

  2. Floats (float): Numbers with a decimal point or numbers written in scientific notation.

     
  3. Strings (str): Sequences of characters enclosed in single or double quotes.

  4. Booleans (bool): Logical values representing True or False.

  5. Lists (list): Ordered collections of items. Items can be of different types.

  6. Tuples (tuple): Similar to lists but immutable (cannot be modified once created).

     
  7. Dictionaries (dict): Unordered collections of key-value pairs.

  8. Sets (set): Unordered collections of unique items.

  9. NoneType (None): Represents the absence of a value or a null value.

Not only is Python dynamically typed, meaning you don’t explicitly declare the variable type, but also the interpreter infers the type based on the value assigned to it. Additionally, Python supports type hints, which allow you to provide optional type information in your code for better readability and tools like linters and static analyzers.

type() is a built-in function that helps you find the class type of the given variable.

You can use the type() function to get the type and print it with print().

For example, below shows you how to print different objects in Python.

a = 1
b = "string"
c = [0, 1, 2]
d = (0, 1, 2)
e = {"key":"value"}
f = 1.0
g = {"set"}

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))

#Output:
<class 'int'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'float'>
<class 'set'>

Hopefully this article has been useful for you to learn how to print the type of a variable in Python.

 

Print
“Large printing machine“/ CC0 1.0

Other Articles You'll Also Like:

  • 1.  Examples of Recursion in Python
  • 2.  String Manipulation: Remove String Parentheses Using Python
  • 3.  Get Last Character in String in Python
  • 4.  How to Divide Two Numbers in Python
  • 5.  Difference Between read(), readline() and readlines() in Python
  • 6.  Create List of Odd Numbers in Range with Python
  • 7.  Python min() function – Get Minimum of List or Dictionary with min() Function
  • 8.  Create Empty File with Python
  • 9.  Using Python to Read File Character by Character
  • 10.  Python max() function – Get Maximum of List or Dictionary with max() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy