Skip to main content

How to Check the Type of a Value in Python with type()

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Python decides what kind of value a variable holds, a whole number, a decimal, some text or a list, and the kind decides what you can do with it. When a value doesn't behave the way you expect, ask Python what it is with type(). This guide does that on Python 3.15.2.

Syntax​

Syntax
type(<value>)
PartWhat it means
<value>Any value, variable or expression
type(...)Returns the value's type, such as int. Printed, it shows as <class 'int'>

The Python documentation says: "With one argument, return the type of an object." In Python, a type is a class, which is why it prints with the word class.

The Example​

A script asks for the type of four values: a whole number, a decimal, a piece of text and a list.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

type_check.py
print(type(42))
print(type(3.5))
print(type("hello"))
print(type([1, 2, 3]))

The type_check.py script in a macOS-style code window: print of type(42), type(3.5), type("hello") and type([1, 2, 3]).

Step 2: Run It​

Terminal
uv run python type_check.py
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>

The terminal after uv run python type_check.py: class int, class float, class str and class list.

42 is an int (a whole number), 3.5 a float (a decimal), "hello" a str (text) and [1, 2, 3] a list. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
type(42) == "int"It printed False: a type isn't texttype(42) == int, which printed True
Testing a type with == when subclasses counttype(True) == int printed False, though True is a kind of intisinstance(True, int), which printed True

FAQ​

How do I get just the name, without <class ...>?​

Read the type's __name__: type(42).__name__ printed int.

Should I use type() or isinstance() in an if?​

isinstance(). The documentation says: "The isinstance() built-in function is recommended for testing the type of an object, because it takes subclasses into account." type() is best for finding out what a value is.

Conclusion​

type() reported int for 42, float for 3.5, str for "hello" and list for [1, 2, 3]. To test a type in code, isinstance() also accepts subclasses.

Additional Resources​