TIMEX TIMEX - 5 months ago 37
Python Question

How do I check if a string is unicode or ascii?

What do I have to do in Python to figure out which encoding?

Answer

In Python 3, all strings are sequences of Unicode characters. There is a bytes type that holds raw bytes.

In Python 2, a string may be of type str or of type unicode. You can tell which using code something like this:

def whatisthis(s):
    if isinstance(s, str):
        print "ordinary string"
    elif isinstance(s, unicode):
        print "unicode string"
    else:
        print "not a string"