How can you print a string using raw_unicode_escape encoding in python 3?

Posted by Sorin Sbarnea on Stack Overflow See other posts from Stack Overflow or by Sorin Sbarnea
Published on 2010-06-14T15:43:34Z Indexed on 2010/06/14 15:52 UTC
Read the original article Hit count: 272

Filed under:
|
|

The following code with fail in Python 3.x with TypeError: must be str, not bytes because now encode() returns bytes and print() expects only str.

#!/usr/bin/python
from __future__ import print_function
str2 = "some unicode text"
print(str2.encode('raw_unicode_escape'))

How can you print a Unicode string escaped representation using print()? I'm looking for a solution that will work with Python 2.6 or newer, including 3.x

© Stack Overflow or respective owner

Related posts about python

Related posts about unicode