W3docs

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

The 'b' character in front of a string literal indicates that the string is a bytes literal.

The 'b' character in front of a string literal indicates that the string is a bytes literal. It is used to indicate that the string should be treated as a sequence of bytes rather than a sequence of characters.

For example:

A bytes lateral in Python

b'hello world' # This is a bytes literal

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

Alternatively, you can use the bytes() function to create a bytes object:

Create a bytes object using the bytes function in Python

bytes('hello world', 'utf-8') # This creates a bytes object from the string 'hello world' using the UTF-8 encoding

It's important to note that in python 3, strings are unicode by default, so it's not necessary to use the 'b' character or the bytes() function unless you specifically need to work with bytes.