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'
3
  • Using Python 3? Commented Aug 23, 2013 at 23:36
  • No, I'm still using Python 2.7 Commented Aug 23, 2013 at 23:38
  • I think [this][1] comes close to what I was looking for. [1]: stackoverflow.com/questions/2596714/… Commented Aug 30, 2013 at 21:01

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

But why doesn't it use ascii instead since it is the default encoding and I haven't defined any other encoding?
@user2635863: Because there's isn't any ascii encoding for ö.
@user2635863 because we're not encoding anything. And what would it mean for it to "use ascii" anyway?
@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?
This is a function of your terminal, not of Python.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.