Reputation: 1534
How to print strings escaped?
Example:
text_str = "żółć"
text_unicode = u"żółć"
print function(text_str)
'\xc5\xbc\xc3\xb3\xc5\x82\xc4\x87'
print function(text_unicode)
u'\u017c\xf3\u0142\u0107'
so, what function should I use?
updated I need more than escape, because I need this in log, and throwing escaped sometimes leads me to unescaped results
Upvotes: 1
Views: 127
Reputation: 180512
You want the repr
output:
In [1]: text_str = "żółć"
In [2]: text_unicode = u"żółć"
In [3]: print repr(text_str)
'\xc5\xbc\xc3\xb3\xc5\x82\xc4\x87'
In [4]: print repr(text_unicode)
u'\u017c\xf3\u0142\u0107'
There are also 'string_escape'
and 'unicode_escape'
:
text_str = "żółć"
text_unicode = u"żółć"
print text_str.encode('string_escape')
print text_unicode.encode('unicode_escape')
But encoding 'unicode_escape'
will give you a str not unicode
Upvotes: 2