Q:

Take an unknown format string and return a UTF-8 string

The following two strings are different in format but identical in data:
str1 = '\xd7\x91\xd7\xa8\xd7\xa7'
str2 = u'\u05d1\u05e8\u05e7'

I need to implement the following function:

Take a string in either one of the formats above as input
Return the equivalent string in the first format as output

I know that I can encode a string from the second format into the first format.
But how do I determine that the input string is indeed given in the second format?

A:

For Python 3.x, the right thing to do would probably be to try to call encode. (For 3.5+, this will hopefully be as simple as try s.encode('utf-8') except AttributeError: s.)
But for 2.x, the encode will succeed even on str1—by first decoding the UTF-8 string as ASCII (or whatever sys.getdefaultencoding() returns) so it can re-encode it, so you definitely don't want that.
When there is no way to safely EAFP, you have no choice but to LBYL. So:
if isinstance(s, unicode):
    return s.encode('utf-8')
else:
    return s

Note that I'm using isinstance here, not calling type and comparing. As PEP 8 says:

Object type comparisons should always use isinstance() instead of comparing types directly.

Why? Because instances of subtypes (subclasses, classes registered with ABCs, etc.) are, by definition, supposed to always count as instances of their supertypes. There are some rare cases where you explicitly need to break that rule, in which case type comparisons are what you want. But otherwise, don't use them.

