Multiple ways to Convert Bytes to/from strings in Python

The string is a group of characters(or Unicode) enclosed in single or double quotes in Python.

Byte is a binary string that contains the value of a string prefixed with a b(utf-8) or a(ASCII) character.

For example

str = "hello"  # String variable
utf8bytes = b"hello" # bytes variable with utf-8 encoded
asciibytes = a "hello" # bytes variable with ASCII encoded

Both hold different types of values, Automatic conversion is not possible. manually need conversion from/to string to binary string or bytes in Python

Default encoding in Python 2 is ASCII and Python 3 is utf-8.

Let’s see an example

Python Convert String to Byte Example

Multiple ways we can convert String to Byte types in Python

  • use the String encode function

encode function converts the string into bytes.



encoding and errors are optional parameters.

If there are no options, From Python 3 onwards, the Default encoding is utf-8, For Python 2 users, ASCII is the default encoding and does not work in Python 2.

you can change encoding to other options latin1,utf_32 etc.


print(result); # b'hello'
print(type(result)); # <class 'bytes'>
  • use byte constructor

The byte constructor takes a string variable and encoding returns bytes of a string value.


b= bytes(str,'utf-8');
print(b); # b'hello'
print(type(b)); #  # <class 'bytes'>
  • use codes library encode function code is a third-party library that provides encode and decode functions for string bye conversion.

First, You have to import into the code.

  import codecs

The encode function in the codecs library encodes the string into bytes.

codecs.encode(byte_obj, [encoding='utf-8'], [errors='strict'])

encoding and errors are optional parameters

Here is an example

import codecs
bytes = b"hello"
codecs.encode(bytes) #'hello'

Python Convert Byte to String Example

Multiple ways we can parse byte to String in Python

  • use the str constructor string convert takes bytes and converted them to a string with a given encoding

str(bytes_string, [encoding], [encoding])


bytes= b 'hello'
s=str(bytes_string, 'utf-8', 'ignore')
print(s) # `hello`
  • use the byte decode function

The decode function takes bytes types and converts into string



Optional parameters are encoding and error, if omitted, takes utf-8 as default in 3 versions. Example

b = b"hello"
str = b.decode("utf-8", "ignore") 
print(str)# hello
  • using codes library decode function

First import the codes name into the python code

  import codecs

The decode function in the codecs library decodes bytes into a string.

codecs.decode(str_obj, [encoding='utf-8'], [errors='strict'])

encoding and errors are optional parameters

Here is an example

import codecs
str = "hello"
codecs.decode(str) # b'hello'


String.encode() without parameters are preferable compared with other approaches in terms of performance and easy to use in 3 version

bytes.decode() function is good compared with others in performance