Home > Backend Development > Python Tutorial > The difference between python2 and python3 strings

The difference between python2 and python3 strings

(*-*)浩
Release: 2019-07-09 10:19:50
Original
2355 people have browsed it

In Python2, strings cannot fully support international character sets and Unicode encoding. To work around this limitation, Python 2 uses a separate string type for Unicode data. To enter a Unicode string literal, add 'u' before the first quotation mark. Ordinary strings in Python 2 are actually encoded (non-Unicode) byte strings. In Python3, there is no need to add this prefix character, otherwise it will be a syntax error, because all strings are already Unicode encoded by default.

The difference between python2 and python3 strings

But there is one more kind of string in python3 (recommended learning: Python video tutorial)

type(b'132') => byte型
Copy after login

And in Writing like this in python2 will report an error

So obviously, there will be a big difference in the use of decode and encode

decode in python2 converts str type to unicode type

Decode in python3 is to convert byte type into str type

Take python3 as an example:

src = ‘你好世界’
Copy after login

The str at this time is str type. If you need to convert it to byte type, you can Pass:

src =  src.encode('utf-8')
Copy after login

At this time, src is already of byte type. If you want to convert it back to str, use it directly:

src = src .decode()
Copy after login

In addition, the open function in python3 adds an encoding parameter, and the default is UTF-8, that is, when the opened file handle is read or written, only str characters containing unicode format are received.

If you pass in a binary file at this time, an error will be reported, for example:

with open('a.bin', 'w') as f:
f.write('xxx')
Copy after login

If you want to read and write binary files, you need to specify the opening method as 'wb' or 'rb'

In addition, if you find that the webpage cannot be displayed properly when crawling it, you need to transcode the content.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of The difference between python2 and python3 strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template