The b denotes a byte string. Bytes are the actual data. Strings are an abstraction. If you had multi-character string object and you took a single character, it would be a string, and it might be more than 1 byte in size depending on encoding.
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.
prefix - String or tuple of strings to be checked. start (optional) - Beginning position where prefix is to be checked within the string. end (optional) - Ending position where prefix is to be checked within the string.
A prefix is the beginning letter of a word or group of words. Example: The word “unhappy” consists of the prefix “un." Given a query, string s , and a list of all possible words, return all words that have s as a prefix. 6.
The b
prefix signifies a bytes
string literal.
If you see it used in Python 3 source code, the expression creates a bytes
object, not a regular Unicode str
object. If you see it echoed in your Python shell or as part of a list, dict or other container contents, then you see a bytes
object represented using this notation.
bytes
objects basically contain a sequence of integers in the range 0-255, but when represented, Python displays these bytes as ASCII codepoints to make it easier to read their contents. Any bytes outside the printable range of ASCII characters are shown as escape sequences (e.g. \n
, \x82
, etc.). Inversely, you can use both ASCII characters and escape sequences to define byte values; for ASCII values their numeric value is used (e.g. b'A'
== b'\x41'
)
Because a bytes
object consist of a sequence of integers, you can construct a bytes
object from any other sequence of integers with values in the 0-255 range, like a list:
bytes([72, 101, 108, 108, 111])
and indexing gives you back the integers (but slicing produces a new bytes
value; for the above example, value[0]
gives you 72
, but value[:1]
is b'H'
as 72 is the ASCII code point for the capital letter H).
bytes
model binary data, including encoded text. If your bytes
value does contain text, you need to first decode it, using the correct codec. If the data is encoded as UTF-8, for example, you can obtain a Unicode str
value with:
strvalue = bytesvalue.decode('utf-8')
Conversely, to go from text in a str
object to bytes
you need to encode. You need to decide on an encoding to use; the default is to use UTF-8, but what you will need is highly dependent on your use case:
bytesvalue = strvalue.encode('utf-8')
You can also use the constructor, bytes(strvalue, encoding)
to do the same.
Both the decoding and encoding methods take an extra argument to specify how errors should be handled.
Python 2, versions 2.6 and 2.7 also support creating string literals using b'..'
string literal syntax, to ease code that works on both Python 2 and 3.
bytes
objects are immutable, just like str
strings are. Use a bytearray()
object if you need to have a mutable bytes value.
This is Python3 bytes
literal. This prefix is absent in Python 2.5 and older (it is equivalent to a plain string of 2.x, while plain string of 3.x is equivalent to a literal with u
prefix in 2.x). In Python 2.6+ it is equivalent to a plain string, for compatibility with 3.x.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With