How to check if type of a variable is string?

In Python 3.x, the correct way to check if s is a string is

isinstance(s, str)

The bytes class isn’t considered a string type in Python 3.

In Python 2.x, the correct check was

isinstance(s, basestring)

basestring is the abstract superclass of str and unicode. It can be used to test whether an object is an instance of either str or unicode.

Leave a Comment