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.

Answer from agf on Stack Overflow
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'>
🌐
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).
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
Converting bytes to string to bytes
How can I convert bytes to string to bytes back? Here’s what I’m trying: from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding message = b"A message I want to sign" signature = private_key.sign( message, padding.PSS( mgf=padding... More on discuss.python.org
🌐 discuss.python.org
8
0
October 28, 2021
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
converting string of bytes into actual bytes.
use Response.content, which returns a bytes object rather than a str object like Response.text See: http://docs.python-requests.org/en/latest/user/quickstart/#binary-response-content More on reddit.com
🌐 r/learnpython
4
2
May 16, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-string-to-bytes
Convert String to bytes-Python - GeeksforGeeks
It turns a string into a sequence of bytes using a specified encoding format (default is UTF-8). ... Explanation: encode() method converts a string to bytes using the specified encoding, here "utf-8", allowing the string to be processed as binary ...
Published   July 11, 2025
🌐
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 most common way to convert a string into bytes is by using the encode() method. This method takes a string and an encoding scheme as arguments and returns a byte object that represents the encoded string.
🌐
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.
Find elsewhere
🌐
Devace Technologies
devacetech.com › home › insights › string to bytes conversion in python-2025 manual
String to Bytes Conversion in Python-2025 Manual
July 28, 2025 - Some key characteristics of bytes include that they are developed by encoding a string or directly using b’’, store binary data, are used for file storage, encryption, networking, and I/O. ... python text = “ Encrypt this ” byte_data = text . encode ( “ utf-8 " ) print ( byte_data ) # Output : b ‘ Encrypt this ’ · If you are new to programming, you must be wondering why it’s required to convert a string into bytes in Python in the first place.
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
January 20, 2025 - The difference between bytes and bytearray is that bytes objects are read-only, while bytearray objects are mutable. You convert a Python string to bytes using the str.encode() method, the bytes() function, or the codecs module.
🌐
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.
🌐
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 - We specify the encoding format as utf-8 using the encoding parameter. Finally, we print the resulting string to the console. We can also use the bytes() constructor, a built-in Python function used to create a new bytes object.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.4 documentation
If neither encoding nor errors is given, str(object) returns type(object).__str__(object), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a __str__() method, then str() falls back to returning repr(object). If at least one of encoding or errors is given, object should be a bytes-like object (e.g.
🌐
Sentry
sentry.io › sentry answers › python › convert a string to bytes in python
Convert a string to bytes in Python | Sentry
July 15, 2023 - my_string = "A string to turn into bytes." print(type(my_string)) # will print "<class 'str'>" my_bytes = my_string.encode() print(type(my_bytes)) # will print "<class 'bytes'>"
🌐
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 format.
🌐
Mimo
mimo.org › glossary › python › bytes
Mimo: The coding platform you need to learn Web Development, Python, and more.
You use them when dealing with ... data types helps prevent encoding bugs when mixing unicode and text encodings. ... A bytes object is defined by prefixing a string literal with a b....
🌐
Analytics Vidhya
analyticsvidhya.com › home › 7 ways to convert string to bytes in python
7 Ways to Convert String to Bytes in Python - Analytics Vidhya
February 7, 2024 - Learn the importance of converting strings to bytes in Python programming. Explore 7 methods for this crucial process and more.
🌐
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 › convert-hex-string-to-bytes-in-python
Convert Hex String to Bytes in Python - GeeksforGeeks
July 23, 2025 - You can convert a hexadecimal string into bytes using list comprehension in a single line by splitting the hex string into pairs of characters, converting each pair to its decimal equivalent and then converting the result into a bytes object.