What does the 'b' character do in front of a string literal in Python?

PythonServer Side ProgrammingProgramming

A prefix of 'b' or 'B' is ignored in Python 2.

In Python 3, Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

Python 3.x makes a clear distinction between the types:

str = '...' literals = a sequence of Unicode characters (UTF-16 or UTF-32, depending on how Python was compiled)

bytes = b'...' literals = a sequence of octets (integers between 0 and 255)

raja
Published on 12-Dec-2017 12:55:21
Advertisements