views:

71

answers:

4

How can I print a string as a sequence of unicode codes in Python?

Input: "если" (in Russian).

Output: "\u0435\u0441\u043b\u0438"

+6  A: 

This should work:

>>> s = u'если'
>>> print repr(s)
u'\u0435\u0441\u043b\u0438'
DisplacedAussie
+3  A: 

Code:

txt = u"если"
print repr(txt)

Output:

u'\u0435\u0441\u043b\u0438'
Michał Niklas
+1  A: 
a = u"\u0435\u0441\u043b\u0438"
print "".join("\u{0:04x}".format(ord(c)) for c in a)
Torsten Marek
A bit overkill, though;)
Torsten Marek
Sure, but it gives unicode also for ascii-characters, which may be desireable. But you should have had a ''.join() around that list comprehension. ;)
Lennart Regebro
@Lennart: Yes, thanks for that. Extracting the string from the return value of repr is a bit tedious (and is gonna change in Python 3, because the u prefix is gone).
Torsten Marek
A: 

If you need a specific encoding, you can use :

txt = u'если'
print txt.encode('utf8')
print txt.encode('utf16')
lightman