How to find out if a Python object is a string?

How can I check if a Python object is a string (either regular or Unicode)?

15 s
15

Python 3

In Python 3.x basestring is not available anymore, as str is the sole string type (with the semantics of Python 2.x’s unicode).

So the check in Python 3.x is just:

isinstance(obj_to_test, str)

This follows the fix of the official 2to3 conversion tool: converting basestring to str.

Leave a Comment