Home »
Python
What does the 'b' character do in front of a string literal in Python?
Here, we are going to learn how to make a variable type as byte – we will also learn what does the 'b' character do in front of a string literal in Python?
Submitted by Sapna Deraje Radhakrishna, on February 01, 2020
Consider the following examples,
# variable declarations
test_str = 'string'
test_bytes = b'string'
# printing the types
print(type(test_str))
print(type(test_bytes))
Output
<class 'str'>
<class 'bytes'>
As per the above example, the prefix of 'b' character to a string, makes the variable of type bytes.
Before version 3, python always ignored the prefix 'b' and in the later version, bytes variable are always prefixed with ‘b’. They may contain ASCII characters, bytes with a numeric value of 128 or greater must be expressed with escapes.
The bytes are the actual data. Strings are an abstraction.