Technology3 min read

Significance of prefix 'b' in a string in Python

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

If you know python well, then this is something you must have already came accross, but if you are a beginner then this article will improve your knowledge of python datatypes.

Let's start with a simple example, consider that you have two strings:

my_string  = ‘A new string’

# and 2nd string

my_string_one = b’A new string’

What do you think, is my_string and my_string_one different?

No? Yes? Ok, here's a hint, the difference lies in the datatype.

Lt's check the type of both the string variables,

my_string  = 'A new string'

print(type(my_string))

my_string_one = b'A new string'

print(type(my_string_one))

Output:

<class 'str'>
<class 'bytes'>

In the output above, str means a literal that has a sequence of Unicode characters (encoded in UTF-16 or UTF-32, and this entirely depends on compilation of Python) whereas bytes mean literals that represent integers between 0 and 255 (also known as octets).

So, by adding the prefix b in front of a normal string, we changed its datatype from string to bytes.


When do I use strings and when do I use bytes?

In Python 2.x, both str and bytes datatype is a Byte type object but in Python 3.x this is changed now. The main difference between bytes and string is that bytes is machine redable and string is human readable and eventually string is also converted into bytes for processing. When we create a bytes datatype object in python that is directly stored on the disk while a string object is first encoded and then stored on disk.

Strings can be used to represent data like special characters, characters, words or almost anything in ahuman readable form hence it should be used in programs whereas Bytes should be used to represent low-level binary data structures.


Can I convert bytes to string and vice-versa?

Yes, it is possible.

String can be converted to bytes with the help of a process known as encoding.

Bytes can be converted to strings with the help of a process known as decoding.

Consider the following example:

print('Öl')

'Öl'.encode('UTF-8')   # Output b'\xc3\x96l'

len('Öl'.encode('UTF-8'))   # Output 3

b'\xc3\x96l'.decode('UTF-8')    # Output 'Öl'

len(b'\xc3\x96l'.decode('UTF-8'))   # Output 2

In the code above, the first line prints a German word, note the 2 dots over the letter O. The length of this string is 2 i.e it has 2 characters.

Since this is a string, it has been encoded using the encode() in the next line, which gives output b'\xc3\x96l'.

The length of this encoded string is 3 bytes which has been represented in the third line of code.

If we wish to convert a byte to a string, decoding must be done and that has been represented in the fourth line.

The length of the resultant decoded string is 2 characters.


Conclusion

In this post, we understood the significance of encoding and decoding string and byte type objects. Also, we learned what is the significance of the b prefix used with strings in some programs.

You may also like:

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next