Tried to print hindi letter and weird output
I tried to print a Hindi letter using the print command and then I saw this weird \xe0\
... stuff. But if I don't use print and print them using only quotes, the output works as expected ... why?
This works great:
"This is ऋ ॠ ऌ"
However, with printing, it doesn't work fine:
var = "This is ऋ ॠ ऌ"
print "Again : %r" % var
And I am getting output as:
Again : This is \xe0\xa4\x8b \xe0\xa5\xa0 \xe0\xa4\x8c'
Why is that?
NOTE:
# -*- coding: utf-8 -*-
# is included
+3
source to share
1 answer
%r
stands for repr
, so it calls repr()
on your line ...
>>> class Demo:
... def __repr__(self):
... return '(repr called)'
... def __str__(self):
... return '(str called)'
...
>>> d = Demo()
>>> repr(d)
'(repr called)'
>>> str(d)
'(str called)'
>>> '%r %s' % (d, d)
'(repr called) (str called)'
... which apparently you don't need. Use instead %s
:
>>> print '%r' % 'ऋ ॠ ऌ'
'\xe0\xa4\x8b \xe0\xa5\xa0 \xe0\xa4\x8c'
>>> print '%s' % 'ऋ ॠ ऌ'
ऋ ॠ ऌ
+4
source to share