Use isinstance to test for Unicode string

A.D picture A.D · Jul 1, 2014 · Viewed 23.2k times · Source

How can I do something like:

>>> s = u'hello'
>>> isinstance(s,str)
False

But I would like isinstance to return True for this Unicode encoded string. Is there a Unicode string object type?

Answer

Martijn Pieters picture Martijn Pieters · Jul 1, 2014

For Python2, you can use basestring to test for both:

isinstance(unicode_or_bytestring, basestring)

basestring is only available in Python 2, and is the abstract base type of both str and unicode.

If you wanted to test for just unicode, then do so explicitly:

isinstance(unicode_tring, unicode)

For Python 3, test for str only:

isinstance(unicode_or_bytestring, str)

or, if you must handle bytestrings, test for bytes separately:

isinstance(unicode_or_bytestring, bytes)

The two types are deliberately not exchangible; use explicit encoding (for str to bytes) and decoding (bytes to str) to convert between the types.