I\'m aware that I can use: isinstance(x, str) in python-3.x but I need to check if something is a string in python-2.x as well. Will isinstance(x, str)
isinstance(x, str)
The most terse approach I've found without relying on packages like six, is:
try: basestring except NameError: basestring = str
then, assuming you've been checking for strings in Python 2 in the most generic manner,
isinstance(s, basestring)
will now also work for Python 3+.