Working with UTF-8 encoding in Python source [duplicate] Working with UTF-8 encoding in Python source [duplicate] python python

Working with UTF-8 encoding in Python source [duplicate]


In the source header you can declare:

#!/usr/bin/env python# -*- coding: utf-8 -*-....

It is described in the PEP 0263:

Then you can use UTF-8 in strings:

#!/usr/bin/env python# -*- coding: utf-8 -*-u = 'idzie wąż wąską dróżką'uu = u.decode('utf8')s = uu.encode('cp1250')print(s)

This declaration is not needed in Python 3 as UTF-8 is the default source encoding (see PEP 3120).

In addition, it may be worth verifying that your text editor properly encodes your code in UTF-8. Otherwise, you may have invisible characters that are not interpreted as UTF-8.


Do not forget to verify if your text editor encodes properly your code in UTF-8.

Otherwise, you may have invisible characters that are not interpreted as UTF-8.