0

In python, when I import sys and type:

>>> sys.getdefaultencoding()
>>> 'ascii'

why is this string automatically encoded as UTF-8?

>>> a = 'ö'
>>> a
>>> '\xc3\xb6'
Paco
  • 4,520
  • 3
  • 29
  • 53
HappyPy
  • 9,839
  • 13
  • 46
  • 68
  • Using [Python 3](http://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit)? – Burhan Khalid Aug 23 '13 at 23:36
  • No, I'm still using Python 2.7 – HappyPy Aug 23 '13 at 23:38
  • I think [this][1] comes close to what I was looking for. [1]: http://stackoverflow.com/questions/2596714/why-does-python-print-unicode-characters-when-the-default-encoding-is-ascii – HappyPy Aug 30 '13 at 21:01

1 Answers1

1

Because the input you provided to python was

 a        =       '   ö       '
\x61\x20\x3d\x20\x27\xc3\xb6\x27

You told a to contain the byte sequence "\xc3\xb6" by putting those two bytes between the quotes in your console input, and so it does.

martineau
  • 119,623
  • 25
  • 170
  • 301
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • But why doesn't it use ascii instead since it is the default encoding and I haven't defined any other encoding? – HappyPy Aug 23 '13 at 23:44
  • 1
    @user2635863: Because there's isn't any ascii encoding for `ö`. – martineau Aug 24 '13 at 00:31
  • @user2635863 because we're not encoding anything. And what would it mean for it to "use ascii" anyway? – hobbs Aug 24 '13 at 01:01
  • @martineau: yes, then I'd have expected an error since there's no ascii encoding for ö. Does this mean that the python interpreter encodes strings automatically to utf-8 (in case of non-ascii characters) even though the default system encoding is ascii? – HappyPy Aug 24 '13 at 09:03
  • @user2635863: On my system, `>>> sys.getdefaultencoding()` also returns `'ascii'`, but after `>>> a = 'ö'`, `>>> a` displays `'\x94'`, not `'\xc3\xb6'`. Also `>>> a.encode('utf-8')` and `>>> 'ö'.encode('utf-8')` both result in `UnicodeDecodeError: 'ascii' codec can't decode byte 0x94 in position 0: ordinal not in range(128)`. – martineau Aug 24 '13 at 13:04
  • 1
    This is a function of your *terminal*, not of Python. – nneonneo Aug 24 '13 at 13:14