11 Answers
+ 9
Using sys.stdout.reconfigure(encoding="utf-8")
+ 7
Because python strings are unicode, I believe that UTF-8 is only required in older systems where letters are still handled as ASCII using 7 bits. That may be the case with any language.
I may be wrong.
strings has encode method that you can use.
"Hello world".encode("utf8");
+ 4
Like Choubada said, UTF-8 is the default encoding in recent versions, so it's not needed. From the docs: "If no encoding declaration is found, the default encoding is UTF-8."
https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations
However, to print UTF-8 or UTF-16 characters, depending on the terminal defaults, it may be necessary to add something to your code:
https://code.sololearn.com/c38hmKFESSf2/?ref=app
+ 4
On Windows 10, Python 3.8? I've had to include Adam Lee's
# -*- coding: utf-8 -*-
..as the top line of my codes in order to read/write text files containing characters with ordinal > 127.
Later I switched to using the files in binary modes instead.
[Note]
The 'strict' parameter in any extended character set (utf8 through utf32) refers to these error handlers:
https://docs.python.org/3/library/codecs.html#error-handlers
[codecs vs wrapper]
Discovery that extended chars worked here was via the "codecs" import but...
...while researching a potential bug I discovered that TextIOWrapper (as Flandre Scarlet is using) was way more capable (I prefer it)
[Trivia]
codec = COder/DECoder
[Unicode HOWTO]
https://docs.python.org/3/howto/unicode.html
+ 3
For the most recent versions, python is automatically in utf-8 so you don't need to specify it
+ 2
You are welcome, Vitalij Bredun ♡ JUMP_LINK__&&__Python__&&__JUMP_LINK Petersburg !
I'm not sure what you mean by the second question. If Adam wishes to discuss more on this specific topic, he can certainly ask here! But if it diverges significantly from the main question of the post, it may be more suitable to make a separate one.
Activity feed posts and direct messages are also options to consider. If the question is about Kuba's code specifically, a comment in the code may be a good idea.
+ 1
Got its answer on stack overflow
# -*- coding: utf-8 -*-
+ 1
Kishalaya Saha pls say a ideas where you advice to post this ask of Adam Lee if this question was discussed but Adam wish to talk talk talk more?
Talking is a useful way to understand something.
+ 1
Kirk Schafer, for files, if you used the encoding keyword of the open() function (reference: https://docs.python.org/3/library/functions.html#open ), did you still have to use the declaration at the top?
0
Kishalaya Saha Thx for your link on the wonderful code.