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 โ€บ converting string into byte array in ascii presentation
r/learnpython on Reddit: Converting String Into Byte Array In ASCII Presentation
April 21, 2023 -

Hello everyone!

I am trying to convert a string into a byte array. If I for example use the string 'Hello' I want to get something like this to store it in a byte array variable:

b'\x48\x45\x4C\x4C\x4F'

What I tried is following:

buf = bytearray(50) # length of text string varies, so I reserved a buffer for a max length of 50 chars

char_arr = [char for char in text]

for i in range(0, len(char_arr)):

buf[i] = hex(ord(char_arr[i]))

This could possibly be total nonsense and I feel like I am making things way too complicated. The variable "text" is the string i read from. I tried converting the string into a char array to read it into the byte array. While doing that I used hex(ord()) to convert the char into its hexadecimal ASCII representation. I just started programming in this language to use CircuitPython on my Raspberry Pi Pico. I get an error called "can't convert str to int". Any suggestions? Thanks in advance!

Discussions

Convert bytes to ascii and back save in Python? - Stack Overflow
I'm Using python 3.5 I have a couple of byte strings representing text that is encoded in various codecs so: b'mybytesstring' , now some are Utf8 encoded other are latin1 and so on. What I want to... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python 3: How to convert a bytearray to an ASCII string - Stack Overflow
I have the following bytearray bytearray(b'S\x00t\x00a\x00n\x00d\x00a\x00r\x00d\x00F\x00i\x00r\x00m\x00a\x00t\x00a\x00.\x00i\x00n\x00o\x00') It should spell out StandardFirmata.ino however, I can't More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert string to ASCII value python - Stack Overflow
In 2021 we can assume only Python 3 is relevant, so... ... (all the above will intentionally raise UnicodeEncodeError if str contains non-ASCII chars. A fair assumption as it makes no sense to ask for the "ASCII value" of non-ASCII chars.) ... A great approach, but bytes() is redundant for a bytes input, and for a string ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting String Into Byte Array In ASCII Presentation
I'm not entirely sure what you're trying to do here. hex() returns a string representation of a hex value, that's not what you want at all. But what are you actually planning to do with the bytearray? I suspect you just want bytearray(text.encode()) but it's hard to tell from this. More on reddit.com
๐ŸŒ r/learnpython
5
1
April 21, 2023
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ converting bytes to ascii or unicode
Converting Bytes to Ascii or Unicode - AskPython
March 25, 2023 - In the first line, we are creating a variable called bsr to store the string which will be encoded by using the bytes() function. We can notice that the utf-8 encoding scheme is used in this code.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-strings-to-bytes-in-python
How to convert strings to bytes in Python
In this section we will use the encode() a built-in method to convert strings to bytes. ... Line 4: We defined a variable with the name of arr, where we stored the utf-8 an encoded string that we generated by using the encode() method.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ binascii.html
binascii โ€” Convert between binary and ASCII
Changed in version 3.3: ASCII-only unicode strings are now accepted by the a2b_* functions. The binascii module defines the following functions: ... Convert a single line of uuencoded data back to binary and return the binary data.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to convert python bytes to ascii string
5 Best Ways to Convert Python Bytes to ASCII String - Be on the Right Side of Change
February 23, 2024 - The characters are then joined to form the final ASCII string. If the default encoding of your Python environment is already set to ASCII, calling decode() without specifying the encoding will convert bytes to an ASCII string.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-string-to-bytes
Convert String to bytes-Python - GeeksforGeeks
Explanation: bytearray() constructor converts a string to a mutable sequence of bytes using the specified encoding, here "utf-8". This allows the byte data to be modified later. In this method, we manually convert each character in the string ...
Published ย  July 11, 2025
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ string-to-bytes-conversion
How to Convert String to Bytes in Python | DataCamp
June 5, 2024 - Therefore, any ASCII character can be represented by seven bits, which is fewer than a single byte. We can create a bytes object by adding b in front of the single, double, or triple quotes we normally use for strings: word_as_bytes = b"Python" print(word_as_bytes) print(type(word_as_bytes))
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-38769.html
bytearray object - why converting to ascii?
November 21, 2022 - I am a new Python developer. Albeit new, I come from a C background. I have a file of bytes I want to read into a bytearray() object. SOME of the byte values are getting converted to an ascii value instead of remaining as a byte value. I must be...
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ p โ€บ bytes-python-built-in-unicode-utf-8-encoding
`bytes`: The Lesser-Known Python Built-In Sequence โ€ข And Understanding UTF-8 Encoding
June 1, 2024 - Several ASCII codes are now obsolete. And since there are only 128 ASCII characters, and their codes range from 0 to 127, they all fit within one byte of data (which has eight bits).
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python convert unicode to bytes, ascii, utf-8, raw string
Python Convert Unicode to Bytes, ASCII, UTF-8, Raw String - Be on the Right Side of Change
June 30, 2021 - PyPi has a unidecode module, it exports a function that takes a Unicode string and returns a string that can be encoded into ASCII bytes in Python 3.x:
๐ŸŒ
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 - Another way to convert a string into bytes is by using the bytearray() function. This function takes a string as an argument and returns a byte array that represents the encoded string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-string-list-to-ascii-values
Convert String List to ASCII Values - Python - GeeksforGeeks
January 19, 2026 - A simple and efficient way to convert strings to ASCII values is by using list comprehension along with the ord() function. ... The ord() function returns the ASCII value of a character.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ bytes-to-string-python
How to Convert Bytes to String in Python | DataCamp
June 12, 2024 - The first 128 UTF-8 codes match the ASCII codes, which is why the bytes object data is decoded to the string "DataCamp" in the example above. However, Unicode contains nearly 150,000 characters. UTF-8 encodes all the non-ASCII characters in Unicode using two or more bytes. ... The bytes object contains two bytes: the integers 195 and 169. Python displays the hexadecimal representation of these 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 - You can use the str() constructor in Python to convert a byte string (bytes object) to a string object. This is useful when we are working with data that has been encoded in a byte string format, such as when reading data from a file or receiving ...
๐ŸŒ
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 - It supports various encodings, such as UTF-8, ASCII, and Latin-1, allowing for flexibility in handling different character sets. However, it is important to note that the encode() method may raise a UnicodeEncodeError if the string contains characters that cannot be encoded in the specified encoding. ... The bytearray() function in Python creates a mutable bytearray object from a string.