Home » Programming » Python » Python Check Variable Type

Checking Type in Python [Guide]

This article explains how to check the type of a variable in the Python programming language.

What are Types?

The type of a variable determines what it can or can’t do.

It determines what value the variable may take and what can be done with that value.

Using the type() Function

Pass a variable or value to the type() function, and the variable’s type will be returned:

myNumber = 4
type(myNumber) # int

myString = 'foo'
type(myString) # str

Using the isinstance() Function

For more examples, check out the similar isinstance() function:

To compare the type of a variable to a known type, you can use the isinstance() function.

Photo of author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment