5

I understand the differences between byte/bytearray and string in Python and how to handle/manipulate/convert these objects but I cannot find real life scenarios/examples where you would prefer to work with bytes instead of strings in the code.

Which are the advantages of byte objects over string objects in Python? and in which real life scenarios should you convert in your code strings into bytes and why?

1
  • bytes are for handling raw bytes... str is for handling text. In early programming languages, and indeed in Python 2, strings were just "byte strings". But in a world with multibyte encoded utf-8 strings, it is better to have two different dedicated types. Commented Jun 7, 2021 at 9:17

2 Answers 2

7

For all modern computer architectures, a byte consists of 8 bits and thus can encode 256 distinct values.

In the ASCII character encoding, there are only 128 different values, with only a subset of those being printable. With UTF-8 it gets a little more complicated, but you end up in a similar problem, that not all byte sequences are representable as a string. So anytime you have a sequence of bytes that is not representable as a string, you have to use bytes() or bytearray.

One example of when you might need to use bytes, is when working with crypto and pseudo-random sequence generation, where you will often end up with a sequence of bytes that cannot be represented 1-to-1 as a string. This is because you want to work with as large as possible an output space when generating pseudo-random numbers and sequences. See for example secrets.token_bytes from the stdlib.

If you want to represent such a sequence as a string, it's possible to encode it into a sequence of bytes that are all inside the ASCII encoding space, but of course, at the cost of using more bytes. For example, you can encode it as hex characters or in base64. Hex has the advantage that the size of the resulting string is always 2 * n_bytes, while base64 is the most efficient way of encoding bytes into ASCII, i.e. it will use the least amount of extra bytes. Note that the secrets stdlib module also gives you convenience functions that does this conversion for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding on to this with another example. Whenever you need to read the contents of a non-text file into memory (like a png image), you cannot read this as a string, because the file may contain bytes that are not representable. So in this case you would open the file in binary mode open('file.png', 'rb') and read into a bytes object instead.
2

in which real life scenarios should you convert in your code strings into bytes and why?

One example is using some compression algorithm which works on bytes rather than str. Take look at lzma built-in module examples, note that it does work with bytes rather than str. In case of a lot of text this allow more effiecient usage of available memory (i.e. saving same text in smaller space).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.