How to check if variable is string with python 2 and 3 compatibility
If you're writing 2.x-and-3.x-compatible code, you'll probably want to use six:
from six import string_typesisinstance(s, string_types)
The most terse approach I've found without relying on packages like six, is:
try: basestringexcept 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+.
What about this, works in all cases?
isinstance(x, ("".__class__, u"".__class__))