1

I've been trying to print a Unicode character in Python 3, but it just doesn't work, I've been looking for a solution here on stackoverflow but nothing works. I have something like this.

print('This is a Unicode character: Ⱥ')

But I got this error message:

enter image description here

I know that Python 3 uses UTF-8 as default encoding, but I don't know what I'm doing wrong here.

Andrés Orozco
  • 2,490
  • 5
  • 32
  • 48

1 Answers1

0

To print Unicode characters correctly, you need these things:

  1. Python must know the source encoding to understand non-ASCII characters in the source code. Python 3 assumes UTF-8 unless told otherwise with a special comment at the top of the file, such as #coding: cp850. Note that the source encoding and the terminal encoding don't have to be the same, and it is fine to use UTF-8 for the source encoding as long as the source file is actually saved in UTF-8.

  2. The Unicode character must be supported by the console's encoding.

  3. The Unicode character must be supported by the console's font.
  4. Use Unicode strings. In Python 3 'string' is a Unicode string. b'string' is a byte string.

Since your error message indicates the console is using Code page 850, you will get a UnicodeEncodeError if you try to print any characters not supported by that code page.

Try this. These are legal cp850 characters:

print('╔═╦═╗')
print('║ ║ ║')
print('╠═╬═╣')
print('║ ║ ║')
print('╚═╩═╝')

This should work for Python 3 on any console/IDE that supports line-draw characters. The US Windows console uses cp437 and European Windows often uses cp850, but both define the characters and the console font supports them.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Thanks for your help. I realized that it is in fact problem of cmd's code page encoding, so, I just change it using CHCP command and UTF-8 code "65001" and it works, but when I close and open cmd again, is not active anymore, and I don't know how to keep it permanently. – Andrés Orozco Nov 23 '14 at 10:15
  • That code page is buggy on Windows. Better to use an IDE that supports UTF8 instead. – Mark Tolonen Nov 24 '14 at 08:39