Python String To Bytes, Bytes To String Example

The bytes type has been added in python 3 to represent some byte sequence. A string consists of multiple characters and operates in character unit. And bytes consists of multiple byte and operates in byte unit. Bytes and string object support almost the same methods except for different data units. Bytes is also an immutable sequence.

Bytes object is only responsible for saving data in byte (binary format) sequence. As for what these data represent, it is entirely up to the program. If the appropriate character set is used, the string can be converted to a byte sequence, conversely, the bytes object can be restored to the corresponding string.

Since bytes object stores the original byte (binary format) data, the bytes object can be used to transmit data on the network, and can also be used to store various binary format files, such as pictures, music, and so on.

1. How To Convert String Object To Bytes Object.

If you want to convert a string to a bytes object, there are three ways.

  1. If the string contents are all ASCII characters, you can convert the string to bytes object by adding b directly before the string.
    # Create an empty bytes object.
    >>> x = bytes()
    >>> 
    >>> x
    b''
    >>> x[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: index out of range # this is because x is an empty bytes object.
    
    # Create bytes object of an empty string object.
    >>> y = b''
    >>> 
    >>> y
    b''
    >>> y[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: index out of range # because empty string object y is also an empty bytes object.
    
    # Convert string 'python' to a bytes object by adding b at the beginning of the string.
    >>> z = b'python'
    >>> 
    >>> z
    b'python'
    >>>
    # get bytes object length. 
    >>> len(z)
    6
    
    # print each byte value in the bytes object.
    >>> z[0]
    112
    >>> z[1]
    121
    >>> z[2]
    116
    >>> 
    >>> z[3]
    104
    >>> z[4]
    111
    >>> z[5]
    110
    >>> z[6]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: index out of range 
    
    # print charactors in the string bytes object.
    >>> z[0:1]
    b'p'
    >>> z[0:2]
    b'py'
    >>> z[0:3]
    b'pyt'
    >>> z[0:4]
    b'pyth'
    >>> z[0:5]
    b'pytho'
    >>> z[0:6]
    b'python'
  2. Call the bytes() function (actually the construction method of bytes) to convert the string into a bytes object according to the specified character set. If no character set is specified, the UTF-8 character set is used by default.
    >>> x1 = bytes('hello world 你好世界', encoding='utf-8')
    >>> 
    >>> x1
    b'hello world \xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c'
  3. Call the encode() method of the string object to convert the string into a bytes object according to the specified character set. The UTF-8 character set is used by default if does not specify a character set.
    >>> y1 = 'hello python 你好'.encode('utf-8')
    >>> 
    >>> y1
    b'hello python \xe4\xbd\xa0\xe5\xa5\xbd'

2. How To Convert Bytes Object To String Object.

If the program obtains the bytes object, it can call the bytes object’s decode() method to decode it into a string.

>>> x1 = bytes('hello world 你好世界', encoding='utf-8')
>>> 
>>> x1
b'hello world \xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c'
>>> 
>>> y1 = x1.decode('utf-8')
>>> 
>>> y1
'hello world 你好世界'
>>> 
>>> y2 = x1.decode()
>>> 
>>> y2
'hello world 你好世界'
>>>

3. Bit vs Byte.

There are two basic concepts in the bottom layer of the computer bit and byte. Bit stands for 1 bit, either 0 or 1, just like a light, either on or off. Byte stands for 1 byte, and 1 byte contains 8 bits.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.