5

After reading: Dive into Python: Unicode Discussion

I got curious to try printing my name in the indic script. I am using v2.7.2 -

>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> name = u'\u0935\u0948\u092D\u0935'
>>> print name
वैभव

I was expecting print name to give me UnicodeError since the defaultencoding is set to ASCII so the auto-coercion to ASCII from Unicode shouldn't work.

What am I missing?

1
  • 2
    Anyone attempting to do serious Unicode work in Python had really best be using Python 3, not the legacy Python 2. But you will need a supplementary regex library, since re is broken on Unicode. And you really can’t do it, because of the horrible UCS‐²⁄₄ heisenbugs in Python. Commented Jul 30, 2011 at 17:56

1 Answer 1

9

print uses sys.stdout.encoding, not sys.getdefaultencoding():

When Python finds its output attached to a terminal, it sets the sys.stdout.encoding attribute to the terminal's encoding. The print statement's handler will automatically encode unicode arguments into str output.

>>> import sys
>>> print(sys.stdout.encoding)
utf-8
>>> print(sys.getdefaultencoding())
ascii
>>> name = u'\u0935\u0948\u092D\u0935'
>>> print name
वैभव
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.