Convert Bytes to Python String

In Python, a byte string is a sequence of bytes. To declare a byte string, you need to start it with the character "b". To convert the bytes string to a Python string, you can use the decode() method of the built-in codecs module. The Python decode() string method decodes a string using the codec registered for encoding. The decode() method returns a string decoded from the given bytes. In this Convert Bytes to Python String example, we use the decode() method to convert bytes to a string. Below are more ways to convert bytes in Python strings with detailed descriptions. Click Execute to run Python Convert Bytes to String Example online and see the result.
Convert Bytes to Python String Execute
my_byte_str = b'I like to eat \xf0\x9f\x8d\x95'

my_str = my_byte_str.decode("utf-8")

print(my_str)
print(my_byte_str)
Updated: Viewed: 209 times

What is Python?

The Python programming language is a high-level interpreted language used to create websites, automate tasks and analyze data. Python is a general-purpose language that can be used to create many different programs and is not specialized in any particular problem. Python has dynamic typing and garbage collection and supports several programming paradigms, including structured, object-oriented, and functional programming. As a result of its comprehensive standard library, it is often referred to as a "batteries included" language. The Python programming language runs on almost all operating systems such as Windows, Linux, and macOS. Python has built-in modules for working with HTTP requests, JSON, strings, and XML out of the box. Python is a straightforward programming language to learn and maintain.

What is a string in Python?

In Python 3, strings are 16-bit Unicode bytes (and 8-bit ASCII bytes in Python 2). Each character is represented by a single byte. A literal string can be enclosed in double quotes or in single quotes. You can use single quotes inside double quotes and vice versa. Python strings are not mutable; they cannot be changed once they are created. String manipulation functions return a new string rather than modifying the existing one. The Python "str" library allows searching, concatenating, reverse, splitting, and comparing strings.

What is byte string?

In Python, a byte string is a fixed-length byte array. Bytes are integers between 0 and 255. A byte string is similar to a Unicode string - but its content is a sequence of bytes, not characters.

Convert bytes to Python string Examples

Below are examples of converting bytes to a string in Python with a detailed description:

Convert bytes to Python string using decode()

To convert the bytes to a Python string, we can use the decode() method:

Convert Bytes to String with decode() Example
my_byte_str = b'Done \xE2\x9C\x85'
my_str = my_byte_str.decode("utf-8")

print(my_str)

# Done ✅

Convert bytes to Python string with codecs

The codecs module defines base classes for standard Python encodings and provides access to Python's internal encoding registry, which controls the process of processing encodings and error handling. First, you must import the codecs module and then call the codecs.decode() method:

Python Import Codecs Module Syntax
import codecs

The following is an example of converting bytes to a Python string using the codecs module:

Convert Bytes to String with Codecs Example
import codecs

my_byte_str = b'Done \xE2\x9C\x85'

print(codecs.decode(my_byte_str))

# Done ✅

Convert bytes to Python string with str()

To convert bytes to a string, you can use the str() function, which takes various values and converts them to strings:

Convert Bytes to String with str() Example
my_byte_str = b'Done \xE2\x9C\x85'
my_str = str(my_byte_str, 'UTF-8')

print(my_str)

# Done ✅

How to convert Python string to bytes?

To convert a Python string to bytes, you can use the encode() method:

Convert Python string to Bytes Example
my_str = "Hello, World"

my_byte_str = my_str.encode(encoding = 'UTF-8')

print(my_byte_str)

# b'Hello, World'

See also