/  Technology   /  What does the ‘b’ character do in front of a string literal in Python?
What does the 'b' character do in front of a string literal in Python

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

 

Let’s take two examples string_1 and string_2 and add a prefix ‘b’ to string_2.

 

Example:

string_1= "I am a string example"
string_2= b'I am a string example'

 

Are these two strings the same?

The answer is NO!

 

Let’s test it by checking the type of each of these strings.

 

Example:

string_1= "I am a string example"
string_2= b'I am a string example'

print(type(string_1))
prunt(type(string_2))

Output:

 

We can clearly say that the type of string_1 is string and string_2 is byte. Therefore, b‘………….’ denotes a byte string.

 

A string literal ‘……..’ is a collection or sequence of Unicode characters(UTF_16, UTF-32, etc depending on how Python was compiled).

 

A byte literal b‘……..’ is a collection of octets ranging from 0 to 255.

 

We use str to represent human-readable text whereas we use bytes to represent machine-readable low-level binary data.

 

When we create a byte type object, then it gets directly stored onto the disk. Whereas when we create a string type object, then it first gets decoded to a byte object and then gets stored. 

 

In all the prior versions of Python 3.x, there was no distinction between str and bytes, both were considered as Byte object

 

In these versions, unicode u‘……’ is a sequence of unicode characters which is equivalent to str in Python 3.x versions. And str ‘……….’ is usually used to represent text encoded in some unspecified encoding, and sometimes it’s also used to represent binary data too.

 

A byte object and a string object are interchangeable. We decode a string to byte and encode a byte to string.

 

To know more about these conversions i.e. A string to Byte and vice-versa, refer to the articles below

String to Byte

Byte to String

 

Leave a comment