Using Unicode string literals as recommended by Nam is correct, but if your terminal uses the cp437 code page, as your output shows, it will not be able to display some of the Unicode characters you want to use. The Windows console does not support UTF-8, this is what you send if you declare coding: utf-8
1 in the source file and do not use Unicode literals. coding: utf-8
declares the encoding of your source file , so make sure you really save your source in UTF-8 encoding.
When you use a Unicode literal, Python interprets the source string in the declared encoding and converts it to a Unicode string. When printing a Unicode string, Python will encode the string in terminal encoding or not have terminal encoding, use ascii
encoding for Python 2 by default.
Example:
# coding: utf8 print '£9.2 billion'
Exit
┬ú9.2 billion £9.2 billion SheffieldΓÇÖs Sheffield’s £9.2 billion Traceback (most recent call last): File "C:\Documents and Settings\metolone\Desktop\x.py", line 10, in <module> print u'SheffieldΓÇÖs' # UnicodeEncodeError. File "C:\dev\python27\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u2019' in position 9: character maps to <undefined>
So, don't expect everything to print all Unicode correctly on the Windows console. Use a Python development environment that supports UTF-8, such as PythonWin (available in the pywin32 extension).
You need two things to correctly display Unicode characters in the Windows console: an encoding that displays the Unicode characters you want to display, and a font that supports the correct character for those characters. For example, if you change the console code page to Windows-1252 ( chcp 1252
) and change the console font to Consolas or Lucida Console instead of Raster Fonts, your original program will work if you use Unicode literals ( p = u"..."
) .
source share