If you want to check with no regard for Python version (2.x vs 3.x), use six
(PyPI) and its string_types
attribute:
import six
if isinstance(obj, six.string_types):
print('obj is a string!')
Within six
(a very light-weight single-file module), it's simply doing this:
import sys
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str
else:
string_types = basestring