Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Fixed the weird syntax highlighting (as a result, the diff looks more extensive than it really is - use view "Side-by-side Markdown" to compare).
Source Link
Peter Mortensen

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict'). For documentation see bytes.decode.

Python 3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>
Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

Note: In Python 3, by default the encoding type is UTF-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict'). For documentation see bytes.decode.

Python 3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

Note: In Python 3, by default the encoding type is UTF-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict'). For documentation see bytes.decode.

Python 3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

Note: In Python 3, by default the encoding type is UTF-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

Active reading [<https://en.wikipedia.org/wiki/History_of_Python#Version_3> <https://en.wikipedia.org/wiki/UTF-8>]. Added the missing punctuation.
Source Link
Peter Mortensen

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict'). For documentation. Click see herebytes.decode.

Python3 Python 3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

NOTENote: In Python3Python 3, by default the encoding type is utf-8UTF-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict') For documentation. Click here

Python3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

NOTE: In Python3 by default encoding type is utf-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict'). For documentation see bytes.decode.

Python 3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

Note: In Python 3, by default the encoding type is UTF-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

Source Link
Shubhank Gupta

We can decode the bytes object to produce a string using bytes.decode(encoding='utf-8', errors='strict') For documentation. Click here

Python3 example:

byte_value = b"abcde"
print("Initial value = {}".format(byte_value))
print("Initial value type = {}".format(type(byte_value)))
string_value = byte_value.decode("utf-8")
# utf-8 is used here because it is a very common encoding, but you need to use the encoding your data is actually in.
print("------------")
print("Converted value = {}".format(string_value))
print("Converted value type = {}".format(type(string_value)))

Output:

Initial value = b'abcde'
Initial value type = <class 'bytes'>
------------
Converted value = abcde
Converted value type = <class 'str'>

NOTE: In Python3 by default encoding type is utf-8. So, <byte_string>.decode("utf-8") can be also written as <byte_string>.decode()

lang-py