3

Suppose I have a .py file with the following contents

print('(ノಠ益ಠ)ノ彡┻━┻')

When called by python3 it prints the angry guy flipping a table, however on python2 we get SyntaxError and need to add a declaration line like # coding: utf-8 in the file to run it.

My question is why doesn't python 3 care about non-ascii characters? I don't see any description of that in PEP 0263. How does the interpreter guess the encoding of the file correctly when I didn't specify that, where is the behaviour documented?

1 Answer 1

6

Python 3 uses utf-8 as the default source code encoding.

Python 2 uses ASCII by default, so unless you explicitly tell Python # -*- coding: utf-8 -*- at the top of your file, it doesn't know how to handle character values above 127.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.