Python: Determine Variable Type Example


In order to determine the type of a variable in Python you can make use of the built-in type() function.

Example 1:
var = "Code2care"

var_type = type(var)

print(var_type)

<class 'str'>


Example 2:
var = 22

var_type = type(var)

print(var_type)

<class 'int'>


Example 3:
var = 22.22

var_type = type(var)

print(var_type)

<class 'float'>


Example 4:
var = [1, 2, 3]

var_type = type(var)

print(var_type)

<class 'list'>


Example 5:
var = (1, 2, 3)

var_type = type(var)

print(var_type)

<class 'tuple'>


Example 6:
var = {"name": "Sam", "age": 22}

var_type = type(var)

print(var_type)

<class 'dict'>


Example 7:
var ={1, 2, 3}

var_type = type(var)

print(var_type)

<class 'set'>


Example 8:
var = False

var_type = type(var)

print(var_type)

<class 'bool'>


Example 9:
var = None

var_type = type(var)

print(var_type)

<class 'NoneType'>

Refer Documentaion: https://docs.python.org/3/library/functions.html#type

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap