Home  >  Article  >  Backend Development  >  How to Use UTF-8 Strings in Python 2 Source Code?

How to Use UTF-8 Strings in Python 2 Source Code?

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 05:45:03530browse

How to Use UTF-8 Strings in Python 2 Source Code?

Encoding UTF-8 Strings in Python Source

One may encounter a syntax error when using Unicode characters in Python 2 source code without declaring the encoding. This is because Python 2 uses ASCII as the default source encoding.

To enable the use of UTF-8 strings in Python 2, declare the encoding in the source code header:

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

This directive, defined in PEP 0263, instructs Python to interpret the source code as UTF-8.

Once the encoding is declared, Unicode characters can be used in strings as shown:

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

u = 'idzie wąż wąską dróżką'
uu = u.decode('utf8')
s = uu.encode('cp1250')
print(s)

In Python 3, UTF-8 is the default source encoding, so this step is unnecessary. Unicode characters can be used directly in source code without any explicit encoding declaration.

The above is the detailed content of How to Use UTF-8 Strings in Python 2 Source Code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn