Decode the bytes object to produce a string:

>>> b"abcde".decode("utf-8")
'abcde'

The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!

Answer from Aaron Maenpaa on Stack Overflow
🌐
Educative
educative.io › answers › how-to-convert-strings-to-bytes-in-python
How to convert strings to bytes in Python
We can use the built-in Bytes class in Python to convert a string to bytes: simply pass the string as the first input of the constructor of the Bytes class and then pass the encoding as the second argument.
Discussions

How to convert string to bytes in Python 3 - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... TypeError: 'str' does not support the buffer interface suggests two possible methods to convert a string to bytes: b = bytes(mystring, 'utf-8') b = mystring.encode('utf-8') What are the differences between them? Which one should I opt for and why? See Convert bytes to a string in Python ... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert a string to bytes?
Or use the bytes() builtin function . An example: x = "abc☺xyz" print(x) y = bytes(x, encoding="utf-8") print(y) More on reddit.com
🌐 r/learnpython
15
6
August 8, 2023
python - Convert byte string to bytes or bytearray - Stack Overflow
I have a string as follows: b'\x00\x00\x00\x00\x07\x80\x00\x03' How can I convert this to an array of bytes? ... and back to a string from the bytes? More on stackoverflow.com
🌐 stackoverflow.com
How to decode a numpy array of encoded literals/strings in Python3?
(That is, I don't want b'') Why not? It shouldn't bother anything. However, if you really want to, use astype(): array1 = array1.astype(str) More on reddit.com
🌐 r/learnpython
14
1
November 2, 2016
🌐
Flexiple
flexiple.com › python › python-string-to-bytes
How to convert Python string to bytes? | Flexiple Tutorials | Python - Flexiple
Note: This method converts objects into immutable bytes, if you are looking for a mutable method you can use the bytearray() method. The encode() method is the most commonly used and recommended method to convert Python strings to bytes.
🌐
DataCamp
datacamp.com › tutorial › bytes-to-string-python
How to Convert Bytes to String in Python | DataCamp
June 12, 2024 - If you're interested in the reverse operation, check out my tutorial on how to convert strings to bytes in Python. Let's start with a short answer for those of you in a hurry.
🌐
DataCamp
datacamp.com › tutorial › string-to-bytes-conversion
How to Convert String to Bytes in Python | DataCamp
June 5, 2024 - In Python, use the .encode() method on a string to convert it into bytes, optionally specifying the desired encoding (UTF-8 by default). ... Get your team access to the full DataCamp for business platform. Strings represent human-readable text and are one of the most basic and important data ...
Top answer
1 of 5
893

If you look at the docs for bytes, it points you to bytearray:

bytearray([source[, encoding[, errors]]])

Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Byte Array Methods.

The optional source parameter can be used to initialize the array in a few different ways:

If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

If it is an integer, the array will have that size and will be initialized with null bytes.

If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array.

Without an argument, an array of size 0 is created.

So bytes can do much more than just encode a string. It's Pythonic that it would allow you to call the constructor with any type of source parameter that makes sense.

For encoding a string, I think that some_string.encode(encoding) is more Pythonic than using the constructor, because it is the most self documenting -- "take this string and encode it with this encoding" is clearer than bytes(some_string, encoding) -- there is no explicit verb when you use the constructor.

I checked the Python source. If you pass a unicode string to bytes using CPython, it calls PyUnicode_AsEncodedString, which is the implementation of encode; so you're just skipping a level of indirection if you call encode yourself.

Also, see Serdalis' comment -- unicode_string.encode(encoding) is also more Pythonic because its inverse is byte_string.decode(encoding) and symmetry is nice.

2 of 5
735

It's easier than it is thought:

my_str = "hello world"
my_str_as_bytes = my_str.encode()
print(type(my_str_as_bytes)) # ensure it is byte representation
my_decoded_str = my_str_as_bytes.decode()
print(type(my_decoded_str)) # ensure it is string representation

you can verify by printing the types. Refer to output below.

<class 'bytes'>
<class 'str'>
Find elsewhere
🌐
AskPython
askpython.com › home › python string to bytes, bytes to string
Python String to bytes, bytes to String - AskPython
February 16, 2023 - Python’s CPython library provides us with bytes() function to convert String to bytes. ... Note: The UTF-8 format is used for the purpose of encoding.
🌐
Real Python
realpython.com › convert-python-bytes-to-strings
How to Convert Bytes to Strings in Python – Real Python
October 5, 2025 - It’s important to be able to manage and handle bytes where they come up. Sometimes they need to be converted into strings for further use or comprehensibility. By the end of this guide, you’ll be able to convert Python bytes to strings so that you can work with byte data in a human-readable ...
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytes
Python bytes()
... Become a certified Python programmer. Try Programiz PRO! ... The bytes() method returns an immutable bytes object initialized with the given size and data. ... # convert string to bytes byte_message = bytes(message, 'utf-8') print(byte_message) # Output: b'Python is fun'
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-string-to-bytes
Convert String to bytes-Python - GeeksforGeeks
Explanation: bytes() constructor converts a string to bytes using the specified encoding, here "utf-8", allowing the string to be processed as binary data. bytearray() constructor works just like bytes(), but it creates a mutable sequence of bytes.
Published   July 11, 2025
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python convert bytes to string
Python Convert Bytes to String - Spark By {Examples}
May 31, 2024 - Always use the encoding that matches the original encoding of the data. ... you can encode a string back to bytes in Python using the encode method. This is the reverse process of decoding bytes to a string.
🌐
Jeremy Morgan
jeremymorgan.com › python › how-to-convert-string-to-bytes-in-python
How to Convert String to Bytes in Python - Jeremy Morgan's
December 8, 2023 - The resulting byte object encoded_bytes is printed as a sequence of bytes. In this article, we have explored three ways to convert a string into bytes in Python: using the encode() method, the bytearray() function, and the struct module.
🌐
w3resource
w3resource.com › python › python-bytes.php
Python Bytes, Bytearray
June 6, 2024 - It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Byte Array Methods. ... If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().
🌐
Analytics Vidhya
analyticsvidhya.com › home › 3 ways to convert bytes to string in python
3 Ways to Convert Bytes to String in Python
March 26, 2025 - A. To convert a string to bytes, use .encode(‘utf-8’) in Python. It transforms the string into a byte object using the specified encoding. ... Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics. ... Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-bytes-to-string-in-python
How to Convert Bytes to String in Python ? - GeeksforGeeks
Converting a hexadecimal string to bytes in Python involves interpreting each pair of hexadecimal characters as a byte. For example, the hex string 0xABCD would be represented as two bytes: 0xAB and 0xCD.
Published   April 14, 2025
🌐
Net Informations
net-informations.com › python › iq › byte.htm
How to convert bytes to string in Python?
convert bytes to string in Python convert string to bytes in Python , We can convert bytes to String using bytes class decode() instance method, So you need to decode the bytes object to produce a string.
🌐
Pierian Training
pieriantraining.com › home › how to convert a string to bytes using python
How to Convert a String to Bytes Using Python
April 27, 2023 - In this example, we first define a variable called hello with a string value ‘Hello World’. We then use the encode() method on it with no arguments specified because we want to use the default encoding scheme (UTF-8). Finally, we print out ...