Just use an empty byte string, b''.

However, concatenating to a string repeatedly involves copying the string many times. A bytearray, which is mutable, will likely be faster:

msg = bytearray()  # New empty byte array
# Append data to the array
msg.extend(b"blah")
msg.extend(b"foo") 

To decode the byte array to a string, use msg.decode(encoding='utf-8').

Answer from Mechanical snail on Stack Overflow
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytes
Python bytes()
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'
🌐
w3resource
w3resource.com › python › python-bytes.php
Python Bytes, Bytearray
June 6, 2024 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-bytes-method
Python bytes() method - GeeksforGeeks
July 11, 2025 - When we pass a list of numbers ... list corresponds to one byte. ... By passing a single integer to bytes(), Python will create a bytes object of that length, filled with zero values (because a byte is initially ...
🌐
Vultr Docs
docs.vultr.com › python › built-in › bytes
Python bytes() - Create Byte Object | Vultr Docs
December 6, 2024 - Provide an iterable of integers ... for 'A', 'B', and 'C' are converted into byte representation. Convert a string to bytes using bytes() with encoding....
🌐
AskPython
askpython.com › home › python bytes()
Python bytes() - AskPython
February 16, 2023 - We need to specify string encoding always! <class 'bytes'> b'Hello from AskPython' byte objects are immutable! An integer zero-initializes that many byte element objects in the array.
🌐
Python
docs.python.org › 3.1 › library › stdtypes.html
5. Built-in Types — Python v3.1.5 documentation
September 6, 2021 - Return an encoded version of the string as a bytes object. Default encoding is the current default string encoding. errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError.
🌐
Flexiple
flexiple.com › python › python-string-to-bytes
How to convert Python string to bytes? | Flexiple Tutorials | Python - Flexiple
Let us look at the code to convert a Python string to bytes. The encoding type we use here is “UTF-8”. #Using the byte() method # initializing string str_1 = "Join our freelance network" str_1_encoded = bytes(str_1,'UTF-8') #printing the encode string print(str_1_encoded) #printing individual bytes for bytes in str_1_encoded: print(bytes, end = ' ')
🌐
Programiz
programiz.com › python-programming › methods › built-in › bytearray
Python bytearray()
If you want the immutable version, use the bytes() method. ... errors (Optional) - if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding) The source parameter can be used to initialize the byte array in the following ways:
Find elsewhere
🌐
Real Python
realpython.com › lessons › defining-bytes-objects-bytes
Defining bytes Objects With bytes() (Video) – Real Python
Another way to define a bytes object is by using the built-in bytes() function. There’s three different approaches that you’re going to practice. The first is you use a function bytes(), and enclosed in arguments you put in a string followed by the…
Published   October 1, 2019
🌐
W3Schools
w3schools.com › python › ref_func_bytes.asp
Python bytes() Function
Remove List Duplicates Reverse ... Study Plan Python Interview Q&A Python Bootcamp Python Training ... The bytes() function returns a bytes object....
🌐
GeeksforGeeks
geeksforgeeks.org › python-bytearray-function
bytearray() function - Python - GeeksforGeeks
February 22, 2025 - Each value must be in the valid byte range (0-255), or Python raises a ValueError. It’s a direct way to construct a bytearray from numeric data. ... The list [72, 101, 108, 108, 111] corresponds to the ASCII values for "Hello". The bytearray is initialized with these values, and you can modify them, as shown with ba[0] = 87.
Top answer
1 of 5
894

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 Reference
python-reference.readthedocs.io › en › latest › docs › functions › bytearray.html
bytearray — Python Reference (The Right Way) 0.1 documentation
an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array. an iterable, it must be an iterable of integers in the range 0-256, which are used to initialize the array · without an argument, an array of size 0 is created. ... Optional. Required if source is a string.
🌐
Python
docs.python.org › 3.1 › library › functions.html
2. Built-in Functions — Python v3.1.5 documentation
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().
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python convert string to bytes
Python Convert String to Bytes - Spark By {Examples}
May 21, 2024 - To convert a string to bytes in Python, use the encode() method. In this program, Apply this method over the string itself with the desired encoding (‘utf-8’ in this case). It returns a byte representation of the string encoded using the ...
🌐
DataCamp
datacamp.com › tutorial › bytes-to-string-python
How to Convert Bytes to String in Python | DataCamp
June 12, 2024 - To convert bytes to strings in Python, we can use the decode() method, specifying the appropriate encoding.
🌐
Real Python
realpython.com › python-bytearray
Python's Bytearray: A Mutable Sequence of Bytes – Real Python
February 18, 2025 - According to the information provided above, you can call bytearray() without any arguments, which creates an empty byte array, or you can pass various values to initialize the array with specific content: Non-negative integer: Creates a zero-filled byte array of the specified length. Iterable of small integers: Creates a byte array from an iterable of integers in the range of 0 to 255, representing the subsequent byte values. Bytes-like object of buffer: Creates a mutable copy of the given bytes-like object or an object implementing the buffer protocol. String and character encoding: Encodes a string into a byte array using the specified character encoding.
🌐
Mimo
mimo.org › glossary › python › bytes
Python Bytes: Syntax, Usage, and Examples
In Python 3, text is stored as str (Unicode) and binary data as bytes; this separation between 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.