Reputation: 33
How can I print a string as a sequence of unicode codes in Python?
Input: "если"
(in Russian).
Output: "\u0435\u0441\u043b\u0438"
Upvotes: 3
Views: 386
Reputation: 86492
a = u"\u0435\u0441\u043b\u0438"
print "".join("\u{0:04x}".format(ord(c)) for c in a)
Upvotes: 1
Reputation: 31
If you need a specific encoding, you can use :
txt = u'если'
print txt.encode('utf8')
print txt.encode('utf16')
Upvotes: 0
Reputation: 54292
Code:
txt = u"если"
print repr(txt)
Output:
u'\u0435\u0441\u043b\u0438'
Upvotes: 3
Reputation: 4694
This should work:
>>> s = u'если'
>>> print repr(s)
u'\u0435\u0441\u043b\u0438'
Upvotes: 9