Checking Type in Python [Guide]

Checking Type in Python

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.

SHARE:
nv-author-image

Brad Morton

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 Reply

Your email address will not be published. Required fields are marked *