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. It is used to indicate that the string should be treated as a sequence of bytes rather than a sequence of characters.
For example:
b'hello world' # This is a bytes literal
Watch a video course
Python - The Practical Guide
Alternatively, you can use the bytes()
function to create a bytes object:
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.