Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print unicode escape sequence from unicode strings in python(3)?

For instance if my string contains - 'नमस्ते' how do I print all the unicode escape sequence for the alphabets in the string.

like image 947
sudha Avatar asked Jul 19 '26 02:07

sudha


1 Answers

If you want the \u escapes for each character (what you'd type to redefine the string in pure ASCII Python code), use the unicode-escape codec:

>>> 'नमसत'.encode('unicode-escape')
b'\\u0928\\u092e\\u0938\\u0924'

If it needs to end up a str, rather than bytes, decode it back as ASCII (and remove the quoting and doubled backslashes on display by printing it):

>>> print('नमसत'.encode('unicode-escape').decode('ascii'))
\u0928\u092e\u0938\u0924
like image 142
ShadowRanger Avatar answered Jul 21 '26 17:07

ShadowRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!