Is there a way to check if the type of a variable in python is a string, like:

isinstance(x,int);

for integer values?

22 s
22

In Python 2.x, you would do

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 str or unicode.


In Python 3.x, the correct test is

isinstance(s, str)

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

Leave a Reply

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