Skip to main content

Command Palette

Search for a command to run...

Strings vs Bytes in Python

Published
•2 min read•View as Markdown

There are significant differences between the concepts of strings and bytes in Python 3. It is critical to recognize this distinction. In Python 3, a string is a sequence of Unicode characters, whereas bytes are a series of raw 8-bit data. It is critical to understand this because you cannot just treat a string as an array of ASCII eight-bit integers. So, let us put this into practice. I've defined two variables, b and s. So, b is a byte sequence, while s is a string. I may also print each one to see what their values are.

def main():
    #defining some values here
    b=bytes([0x41, 0x42, 0x43, 0x44])
    print(b)

    s="This is a string"
    print(s)

if __name__== "__main__":
    main()

And output will be like:

b'ABCD'
This is a string

Process finished with exit code 0

We want to combine the values of b and s together. If we do "print(s+b)" here, the program is going to error out with a TypeError.

It also specifies that it must be str rather than bytes. That's because the print function requires me to give it a string, which I can't just mix with bytes. This is true even when the byte values represent ASCII characters. I can't just treat it as a string; I have to decode the bytes into a string. And I can accomplish that with the built-in "decode" function.

def main():
    #defining some values here
    b=bytes([0x41, 0x42, 0x43, 0x44])
    s="This is a string"
    s2=b.decode('utf-8')
    print(s+s2)


if __name__== "__main__":
    main()

And our output will look like this:

This is a stringABCD

Process finished with exit code 0

More from this blog

B

Blog of İlke Candan Bengi

44 posts

I am writing about Python, machine learning, robotics and data science