Python base python3 str and bytes

Source: Internet
Author: User

Str and bytes are distinguished in Python3, bytes represents a binary byte, str represents a text string, text is always Unicode, is represented by the STR type, and binary data is represented by a bytes type. Python 3 does not mix str and bytes in any implicit way, and the two require a coded conversion when mixed

The line between a string and a byte packet is inevitable, the string can be encoded into a byte packet, and the byte packet can be decoded into a string, the following diagram is very important, be sure to keep in mind:

Example:

>>> ' €20 '. Encode (' Utf-8 ')    # encode (parameter) indicates that the format Type B ' \xe2\x82\xac20 ' # before being converted       is in binary format

' €20 '

str->bytes: encode encoding; encoding is the conversion of a string into a bytecode, involving the internal representation of a string.
bytes->str decoding is the conversion of bytecode into strings, and the bit bits are displayed as characters. 

In purely manipulating string-type data, there is no need to consider strings and binary conversions, only when strings are encoded into byte packets (for example, by using the socket network for development, sending them on the channel; Python3 are all binary transmissions) or decoding strings from byte packets (at programming time, We start to focus on this if the program can only handle text that cannot handle binary and needs to be converted. In the computer, the video is stored in binary format, the text is stored in a string format (it can actually be forced to be stored as a binary).

Python base python3 str and bytes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.