python中将汉字转化成反斜线u(\u)开头的字符串

2016-04-25  本文已影响232人  暂时还没有昵称2222

要求:

'张三' 输出这样的字符串 '\u5f20\u4e09'

python2中代码如下:

u'张三'.encode('unicode-escape')

python3中代码如下:

'张三'.encode('unicode-escape')

如果自己去实现

''.join(r'\u{0:04x}'.format(ord(x)) for x in '张三')
上一篇 下一篇

猜你喜欢

热点阅读