How to display native accents in python 2

Published: April 30, 2018

DMCA.com Protection Status

Example:

>>> s = "Plébiscité sur la toile"
>>> s
'Pl\xc3\xa9biscit\xc3\xa9 sur la toile'
>>> print s
Plébiscité sur la toile
>>> type(s)
<type 'str'>

Change s variable encoding :

 >>> s = u"Plébiscité sur la toile"
 >>> type(s)
 <type 'unicode'>
 >>> s = u"Plébiscité sur la toile".encode('utf-8')
 >>> s
'Pl\xc3\xa9biscit\xc3\xa9 sur la toile'
 >>> type(s)
 <type 'str'>

Another example:

  >>> s = "Plébiscité sur la toile"
  >>> type(s)
  <type 'str'>
  >>> s = unicode(s,'utf-8')
  >>> s
  u'Pl\xe9biscit\xe9 sur la toile'
  >>> type(s)
  <type 'unicode'>

Note In a python script to fix the error message:

#SyntaxError: Non-ASCII character '\x96' in file MyFile.py on line XX, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

just add at the first line of the python scriptL

# -*- coding: utf-8 -*-

References