试图打印印地语字母和奇怪的输出

Tried to print letter of hindi and strange output

我尝试使用 print 命令打印印地语字母,然后我看到了这个奇怪的 \xe0\... 东西。但是,如果我不使用打印并仅使用引号打印它们,输出将按预期工作……为什么这样呢?

这很好用:

"This is ऋ ॠ ऌ"

但是打印效果不佳:

var = "This is ऋ ॠ ऌ"
print "Again : %r" % var

我得到的输出是:

Again : This is \xe0\xa4\x8b \xe0\xa5\xa0 \xe0\xa4\x8c'

为什么会这样?

注意:

 # -*- coding: utf-8 -*- 
 # is included 

%r 代表 repr,所以它在你的字符串上调用 repr()...

>>> 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)'

...这显然是您不想要的。使用 %s 代替:

>>> print '%r' % 'ऋ ॠ ऌ'
'\xe0\xa4\x8b \xe0\xa5\xa0 \xe0\xa4\x8c'
>>> print '%s' % 'ऋ ॠ ऌ'
ऋ ॠ ऌ