Difference between bytes and string in Python

Introduction

On the surface, this question looks innocent but if we dig a bit deeper, things start to get confusing. In this short post, we will provide a summary of differences, however to better understand the topic, I recommend that you check the full article here.

Depending on which version of Python we are using (2.x vs 3.x) strings are handled differently. Not only that but also talking about strings without a basic idea about character encoding makes it even more confusing. Keep in mind that encoding (ex. UTF8) is converting character code points (i.e. a number that represents a character) to byte streams for efficient storage, processing and transmission while decoding is the opposite process.

So what is the difference between bytes and string then ?

Python 2.x

  • Defines immutable strings of type str for bytes data.
  • Defines bytearray data type that can be modified

Take a look at the following example…

Python 3.x

  • Strings (str) are Unicode (not bytes) by default
  • Defines byte and bytearray (mutable) data types

Take a look at the following example…

References

For further reading, check the following related posts…

Thanks for reading.

Add a Comment

Your email address will not be published. Required fields are marked *