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