in python 3:

>>> a=b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>> b = list(a)
>>> b
[0, 0, 0, 0, 7, 128, 0, 3]
>>> c = bytes(b)
>>> c
b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>>
Answer from steel.ne on Stack Overflow
๐ŸŒ
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
Discussions

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
Get single-byte bytes objects from a bytes object
", line 1, in ValueError: empty separator Of course, the result can also be achieved by... More on discuss.python.org
๐ŸŒ discuss.python.org
0
2
December 22, 2023
Convert bytes to a string in Python 3 - Stack Overflow
If you want to convert byte array to string cutting off trailing '\x00' characters the following answer is not enough. Use b'example\x00\x00'.decode('utf-8').strip('\x00') then. 2013-04-16T13:27:01.373Z+00:00 ... Official documentation for this: for all bytes and bytearray operations (methods which can be called on these objects), see here: docs.python... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
๐ŸŒ
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 ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.4 documentation
To get a linear runtime cost, you must switch to one of the alternatives below: if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete ยท if concatenating bytes objects, you can similarly use bytes.join() or io.BytesIO, or you can do in-place concatenation with a bytearray object.
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python โ€บ python-bytes.php
Python Bytes, Bytearray
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().
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Get single-byte bytes objects from a bytes object - Python Help - Discussions on Python.org
December 22, 2023 - A bytes object yield int values when being iterated over: >>> list(b'abc') [97, 98, 99] I find myself wanting something like this: >>> b'abc'.to_list_of_single_bytes() [b'a', b'b', b'c'] I thought that .split() is thโ€ฆ
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-bytes-to-string-how-to-convert-a-bytestring
Python Bytes to String โ€“ How to Convert a Bytestring
April 10, 2023 - The codecs module in Python provides a way to convert data between different encodings, such as between byte strings and Unicode strings. It contains a number of classes and functions that you can use to perform various encoding and decoding operations.
๐ŸŒ
Real Python
realpython.com โ€บ convert-python-bytes-to-strings
How to Convert Bytes to Strings in Python โ€“ Real Python
November 26, 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 ...
๐ŸŒ
Devace Technologies
devacetech.com โ€บ home โ€บ insights โ€บ string to bytes conversion in python-2025 manual
How to convert a string to bytes in Python
July 28, 2025 - This blog will serve as a guide for you to learn how to do string-to-bytes conversion in Python. Before getting into the detailed conversion, letโ€™s understand what strings and bytes in Python are: A string is a sequence of Unicode characters in Python. It may include numbers, punctuation, letters, or even emojis.
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'>
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.4 documentation
February 27, 2026 - 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 Bytearray Operations. 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().
๐ŸŒ
JanBask Training
janbasktraining.com โ€บ community โ€บ python-python โ€บ how-can-i-change-python-string-into-bytes1
how can I change Python string into bytes?
What are the features of Angul... ... Meet the Trailblazers who go above and beyond to help community members succeed with Salesforce.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-bytes-to-string-in-python
How to Convert Bytes to String in Python ? - GeeksforGeeks
The codecs module provides an alternative way to decode bytes, especially useful when dealing with different encodings. ... If you have a list of ASCII values, you can manually convert them into characters and join them into a string.
Published ย  July 23, 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.
๐ŸŒ
Python Guides
pythonguides.com โ€บ convert-string-to-bytes-in-python
How To Convert String To Bytes In Python?
April 7, 2025 - Read How to Print Strings and Variables in Python? The simplest and most widely used approach to convert a string to bytes is using the encode() method.
๐ŸŒ
Pydantic
docs.pydantic.dev โ€บ latest โ€บ concepts โ€บ models
Models | Pydantic Docs
model_validate(): data can be provided either as a dictionary, or as a model instance (by default, instances are assumed to be valid; see the revalidate_instances setting). Arbitrary objects can also be provided if explicitly enabled. The JSON and strings modes can be used with dedicated methods: model_validate_json(): data is validated as a JSON string or bytes object. If your incoming data is a JSON payload, this is generally considered faster (instead of manually parsing the data as a dictionary).