Just use a bytearray() which is a list of bytes.

Python2:

s = "ABCD"
b = bytearray()
b.extend(s)

Python3:

s = "ABCD"
b = bytearray()
b.extend(map(ord, s))

By the way, don't use str as a variable name since that is builtin.

Answer from Pithikos on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ lists to bytes
r/learnpython on Reddit: Lists to Bytes
April 26, 2021 -

I am trying to convert a list into data-type bytes so I can encrypt it. Anyone knows how to do it for a list with strings. Thanks

Here's a example:

list_of_items = ["Cats", "Dogs","Turtle","Humans"]

list_of_items_byte = bytes(list_of_items,"utc-8")

and I get this error:

File "main.py", line 2, in <module>

list_of_items_byte = bytes(list_of_items,"utc-8")

TypeError: encoding without a string argument

Discussions

How to convert a list of bytes (Unicode) to a Python string? - Stack Overflow
I have a list of bytes (8-bit bytes, or in C/C++ language they form a wchar_t type string), they form a Unicode string (byte by byte). How to convert those values into a Python string? I tried a few More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 31, 2018
Convert a list of bytes to a string in Python 3 - Stack Overflow
I'm writing a program in Python 3.4.1 that uses PySerial to test some hardware. Bytes are read from the serial port one at a time, and then appended to list. When the list reaches a certain size, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How Do I Convert a List to a Byte-Like Object?
Try using a list comprehension or a normal for loop to loop through each element of the list and convert it to bytes. Example: my_list = [bytes(item, 'utf-8') for item in my_list] # or, an expanded version for _ in range(len(my_list)): my_list[_] = bytes(my_list[_], 'utf-8') Good luck with your code! More on reddit.com
๐ŸŒ r/learnpython
20
1
July 19, 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
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to convert a python list of bytes to string
5 Best Ways to Convert a Python List of Bytes to String - Be on the Right Side of Change
February 27, 2024 - One standard method for converting a list of bytes to a string in Python involves using the join() method from the string type, combined with list comprehension for decoding each bytes object in the list.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-18372.html
convert a list of string+bytes into a list of strings (python 3)
May 14, 2019 - Hello everyone! I would like to extract the first bytes of a binary file, and convert them into strings, in a list. def binaryMethod(files, lenI): outList = [] for _ in range(lenI): # read the length of the next string (read only the...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.7 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 ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-convert-a-byte-string-to-a-list-of-integers
Python program to convert a byte string to a list of integers - GeeksforGeeks
January 16, 2025 - Byte string is already a sequence of integers (where each byte is an integer between 0 and 255) we can directly convert the byte string to a list of integers using list().
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i convert a list to a byte-like object?
r/learnpython on Reddit: How Do I Convert a List to a Byte-Like Object?
July 19, 2021 -

I am trying to pass a list to an OS command using subprocess.communicate, but can't seem to get it to use my list. I did some researching and thought I was supposed to use bytes to convert my list to a byte-like object, but bytes does not take lists. I am very new to Python and having a hard time figuring this out. My functions is:

def prompt_for_input(list_of_programs, prompt_menu, extra_arguments, prompt_string):
    ## Variable declarations.
    user_selection = None

    ## If extra_arguments were passed, then reform  prompt_menu
    ## to contain the extra_arguments.
    if extra_arguments:
            ## Get the prompt argument from prompt_menu.
            prompt_argument = "-" + prompt_menu.split('-')[1]

            ## Reform prompt_menu to include extra_arguments.
            prompt_argument_index = prompt_menu.index(prompt_argument)
            prompt_menu = prompt_menu[:prompt_argument_index] + extra_arguments +\
                  " " + prompt_menu[prompt_argument_index:]

    ## Add prompt_string to prompt_menu.
    prompt_menu = prompt_menu + " " + prompt_string

    ## Get the user_selection.
    process = subprocess.Popen(shlex.split(prompt_menu), stdout=subprocess.PIPE,\
                           stdin=subprocess.PIPE)

    user_selection = process.communicate(input=bytes(list_of_programs))

I can't seem to find any solid information on how to convert this, could someone point me in the right direction?

Edit

  • Updated the code above to match with master.

๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ python-basic-exercise-94.php
Python: Convert a byte string to a list of integers - w3resource
May 17, 2025 - # Define a string named S. S = "The quick brown fox jumps over the lazy dog." # Print a message indicating the original string. print("Original string:") # Print the original string. print(S) # Create an empty list named nums. nums = [] # Print a message to indicate the conversion of bytes to a list of integers.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ python-program-to-convert-a-byte-string-to-a-list
Python Program to Convert a Byte String to a List
March 27, 2026 - List of ASCII values of an input string: [84, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110, 116] List comprehension provides a concise way to convert a byte string to a list ?
๐ŸŒ
Net Informations
net-informations.com โ€บ python โ€บ iq โ€บ byte.htm
How to convert bytes to string in Python?
Then decoded the bytearray object ... byte values and you want to convert them to a string without using the b prefix, you can use the map() function along with the chr() function....
๐ŸŒ
Real Python
realpython.com โ€บ convert-python-bytes-to-strings
How to Convert Bytes to Strings in Python โ€“ Real Python
July 18, 2026 - Ignoring the differences between bytes and strings could cause a bunch of errors in your code thatโ€™ll lead you to some frustrating debugging sessions. Note: Python restricts bytes literals to ASCII characters only, meaning that something like b"รฉ" would result in a syntax error.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-convert-bytes-to-string-in-python
How to Convert Bytes to String in Python ? - GeeksforGeeks
The str() function of Python returns the string version of the object. ... Itโ€™s a useful alternative, especially when you're not calling it on a byte object directly. The codecs module provides an alternative way to decode bytes, especially ...
Published: April 14, 2025