例如,如果我的字符串包含 - 'नमस्ते',我该如何打印字符串中字母的所有 unicode 转义序列。
最佳答案
如果您希望每个字符的 \u
转义(您键入的内容以在纯 ASCII Python 代码中重新定义字符串),请使用 the unicode-escape
codec :
>>> 'नमसत'.encode('unicode-escape')
b'\\u0928\\u092e\\u0938\\u0924'
如果它需要以 str
结束,而不是 bytes
,decode
将其返回为 ASCII(并删除引号和双反斜杠通过打印显示):
>>> print('नमसत'.encode('unicode-escape').decode('ascii'))
\u0928\u092e\u0938\u0924
关于python - 如何从 python(3) 中的 unicode 字符串打印 unicode 转义序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47566817/