How to Check the Datatype of a Variable in Python

Check the datatype of a variable using Python's built-in `type()` function

Picture of Nsikak Imoh, author of Macsika Blog
Plain background with the text How to Check the Datatype of a Variable in Python
Plain background with the text How to Check the Datatype of a Variable in Python

Table of Content

Sometimes, you might want to check the datatype of a variable in Python.

You can check the datatype of a variable using Python's built-in type() function by passing the variable as an argument, and then check the result.

The output will be the repr of the class.

How to use the type() Function to Check the Datatype of a Variable in Python

Here's a code example on how to check the datatype of a variable usingusing Python's builtin type():

Check an Integer


length = 30

type(length)

Highlighted code sample.

The output will look like this:

<class 'int'>
Highlighted code sample.

Check a Float


pie = 3.142

type(pie)

Highlighted code sample.

The output will look like this:

<class 'float'>
Highlighted code sample.

Check a String


best_color = "RED"

type(best_color)

Highlighted code sample.

The output will look like this:

<class 'str'>
Highlighted code sample.

Check a Set


vowels = {"a", "e", "i", "o", "u"}

type(vowels)

Highlighted code sample.

The output will look like this:

<class 'set'>
Highlighted code sample.

Check a List


vowels = ["a", "e", "i", "o", "u"]

type(vowels)

Highlighted code sample.

The output will look like this:

<class 'list'>
Highlighted code sample.

Check a Tuple


vowels = ("a", "e", "i", "o", "u")

type(vowels)

Highlighted code sample.

The output will look like this:

<class 'tuple'>
Highlighted code sample.

Check a Dict


vowels = {"a": "A", "e": "E", "i": "I", "o": "O", "u": "U"}

type(vowels)

Highlighted code sample.

The output will look like this:

<class 'dict'>
Highlighted code sample.

Wrap Off

That's a candid example of how you can check the datatype of a variable using Python's built-in type() function.

If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.

Get the Complete Code of Python Code Snippets on Github.

Connect with me.

Need an engineer on your team to grease an idea, build a great product, grow a business or just sip tea and share a laugh?