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)
The output will look like this:
<class 'int'>
Check a Float
pie = 3.142
type(pie)
The output will look like this:
<class 'float'>
Check a String
best_color = "RED"
type(best_color)
The output will look like this:
<class 'str'>
Check a Set
vowels = {"a", "e", "i", "o", "u"}
type(vowels)
The output will look like this:
<class 'set'>
Check a List
vowels = ["a", "e", "i", "o", "u"]
type(vowels)
The output will look like this:
<class 'list'>
Check a Tuple
vowels = ("a", "e", "i", "o", "u")
type(vowels)
The output will look like this:
<class 'tuple'>
Check a Dict
vowels = {"a": "A", "e": "E", "i": "I", "o": "O", "u": "U"}
type(vowels)
The output will look like this:
<class 'dict'>
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.