http://docs.python.org/py3k/library/functions.html#ascii
As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x, \u or \U escapes. This generates a string similar to that returned by repr() in Python 2.
And the resulting string will indeed be of type str
rather than bytes
.
Example:
>>> a = '''Ⴊ ⇠ ਐ ῼ இ ╁ ଠ ୭ ⅙ ㈣'''
>>> ascii(a)
"'\\u10aa \\u21e0 \\u0a10 \\u1ffc \\u0b87 \\u2541 \\u0b20 \\u0b6d \\u2159 \\u3223'"
>>> print(ascii(a))
'\u10aa \u21e0 \u0a10 \u1ffc \u0b87 \u2541 \u0b20 \u0b6d \u2159 \u3223'
And if you wanted to trim off the extra quotes, you could just do print(ascii(a)[1:-1])
.
EDIT: As Alex states, you'd have to use repr
in Python 2.6 instead of ascii
. His solution does indeed work for both Python 2 and 3, but if you plan on doing the conversion a lot (and thus would prefer something easier to type multiple times), one possibility is to put a conditional at the start of your program as follows:
import sys
if sys.version_info[0] == 3:
unic = ascii
else:
unic = repr
And then you just use unic
(or whatever you want to call it) wherever you'd use repr
in Python 2 and ascii
in Python 3.
...Though I suppose you could use elif sys.version_info[0] == 2:
instead of else:
if you wanted to be a bit more careful.