If a website uses a language with characters farther back in the Unicode library, UTF-8 will encode all characters as four bytes, whereas UTF-16 might encode many of the same characters as only two bytes. Still, if your pages are filled with ABCs and 123s, stick with UTF-8.
UTF-8 extends the ASCII character set to use 8-bit code points, which allows for up to 256 different characters. This means that UTF-8 can represent all of the printable ASCII characters, as well as the non-printable characters.
Why use UTF-8? An HTML page can only be in one encoding. You cannot encode different parts of a document in different encodings. A Unicode-based encoding such as UTF-8 can support many languages and can accommodate pages and forms in any mixture of those languages.
The first line of each file shoud be #!/usr/bin/env python . This makes it possible to run the file as a script invoking the interpreter implicitly, e.g. in a CGI context. Next should be the docstring with a description.
wherever you need to use in your code chars that aren't from ascii, like:
ă
interpreter will complain that he doesn't understand that char.
Usually this happens when you define constants.
Example: Add into x.py
print 'ă'
then start a python console
import x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "x.py", line 1
SyntaxError: Non-ASCII character '\xc4' in file x.py on line 1,
but no encoding declared;
see http://www.python.org/peps/pep-0263.html for details
A more direct answer:
In Python 3+: you don't need to declare. UTF-8 is the default. Make sure the file is encoded in UTF-8. Some Windows editors don't have it by default. It won't hurt to declare it, and some editors may use it.
In Python 2: always. The default is OS dependent.
And remember: this is just about your source code files. Now in the 3rd millennium the string type does not exist anymore. You must take care of the type text, that is a sequence of bytes and an encoding. You'll still have to define your encoding in all input and output operation. These operations will still be dependent on your environment, so it's still better to follow the rule: Explicit is better than implicit.
Always use UTF-8 and make sure your editor also uses UTF-8. Start your Python script like this if you use Python 27:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
This is a good blog post from Nick Johnson about Python and UTF-8:
http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python By the way, this post was written before he could use:
from __future__ import unicode_literals
When you use non-ascii characters. For instance when I comment my source in norwegian if charachters ØÆÅ occur in the .py it will complain and not "compile".
Whenever text is read or written, encodings come in play. Always. A python interpreter has to read your file as text, to understand it. The only situation where you could get away without having to deal with encodings is when you only use characters in the ASCII range. The interpreter can in this case use virtually any encoding in the world, and get it right because almost all encodings encode these characters to same bytes.
You should not use coding: utf-8
just because you have characters beyond ascii in your file, it can even be harmful. It is a hint for the python interpreter, to tell it what encoding your file is in. Unless you have configured your text editor, the text editor will most likely not save your files in utf-8. So now the hint you gave to the python interpreter, is wrong.
So you should use it when your file is encoded in utf-8. If it's encoded in windows-1252, you should use coding: windows-1252
and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With