The only thing that a computer can store is bytes.

To store anything in a computer, you must first encode it, i.e. convert it to bytes. For example:

  • If you want to store music, you must first encode it using MP3, WAV, etc.
  • If you want to store a picture, you must first encode it using PNG, JPEG, etc.
  • If you want to store text, you must first encode it using ASCII, UTF-8, etc.

MP3, WAV, PNG, JPEG, ASCII and UTF-8 are examples of encodings. An encoding is a format to represent audio, images, text, etc. in bytes.

In Python, a byte string is just that: a sequence of bytes. It isn't human-readable. Under the hood, everything must be converted to a byte string before it can be stored in a computer.

On the other hand, a character string, often just called a "string", is a sequence of characters. It is human-readable. A character string can't be directly stored in a computer, it has to be encoded first (converted into a byte string). There are multiple encodings through which a character string can be converted into a byte string, such as ASCII and UTF-8.

'I am a string'.encode('ASCII')

The above Python code will encode the string 'I am a string' using the encoding ASCII. The result of the above code will be a byte string. If you print it, Python will represent it as b'I am a string'. Remember, however, that byte strings aren't human-readable, it's just that Python decodes them from ASCII when you print them. In Python, a byte string is represented by a b, followed by the byte string's ASCII representation.

A byte string can be decoded back into a character string, if you know the encoding that was used to encode it.

b'I am a string'.decode('ASCII')

The above code will return the original string 'I am a string'.

Encoding and decoding are inverse operations. Everything must be encoded before it can be written to disk, and it must be decoded before it can be read by a human.

Answer from Zenadix on Stack Overflow
Top answer
1 of 10
786

The only thing that a computer can store is bytes.

To store anything in a computer, you must first encode it, i.e. convert it to bytes. For example:

  • If you want to store music, you must first encode it using MP3, WAV, etc.
  • If you want to store a picture, you must first encode it using PNG, JPEG, etc.
  • If you want to store text, you must first encode it using ASCII, UTF-8, etc.

MP3, WAV, PNG, JPEG, ASCII and UTF-8 are examples of encodings. An encoding is a format to represent audio, images, text, etc. in bytes.

In Python, a byte string is just that: a sequence of bytes. It isn't human-readable. Under the hood, everything must be converted to a byte string before it can be stored in a computer.

On the other hand, a character string, often just called a "string", is a sequence of characters. It is human-readable. A character string can't be directly stored in a computer, it has to be encoded first (converted into a byte string). There are multiple encodings through which a character string can be converted into a byte string, such as ASCII and UTF-8.

'I am a string'.encode('ASCII')

The above Python code will encode the string 'I am a string' using the encoding ASCII. The result of the above code will be a byte string. If you print it, Python will represent it as b'I am a string'. Remember, however, that byte strings aren't human-readable, it's just that Python decodes them from ASCII when you print them. In Python, a byte string is represented by a b, followed by the byte string's ASCII representation.

A byte string can be decoded back into a character string, if you know the encoding that was used to encode it.

b'I am a string'.decode('ASCII')

The above code will return the original string 'I am a string'.

Encoding and decoding are inverse operations. Everything must be encoded before it can be written to disk, and it must be decoded before it can be read by a human.

2 of 10
366

Assuming Python 3 (in Python 2, this difference is a little less well-defined) - a string is a sequence of characters, ie unicode codepoints; these are an abstract concept, and can't be directly stored on disk. A byte string is a sequence of, unsurprisingly, bytes - things that can be stored on disk. The mapping between them is an encoding - there are quite a lot of these (and infinitely many are possible) - and you need to know which applies in the particular case in order to do the conversion, since a different encoding may map the same bytes to a different string:

>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-16')
'่“์ฝฏ์บๆพฝ่‹'
>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-8')
'ฯ„oฯฮฝoฯ‚'

Once you know which one to use, you can use the .decode() method of the byte string to get the right character string from it as above. For completeness, the .encode() method of a character string goes the opposite way:

>>> 'ฯ„oฯฮฝoฯ‚'.encode('utf-8')
b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-string-to-bytes
Convert String to bytes-Python - GeeksforGeeks
This allows the byte data to be modified later. In this method, we manually convert each character in the string into its ASCII value using the ord() function, then combine them into a byte sequence.
Published: July 11, 2025
๐ŸŒ
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.
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'>
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ bytes
Python bytes()
Online Python Online JavaScript ... Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... 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') ...
๐ŸŒ
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.
๐ŸŒ
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 - In this example, we define a byte string b"Hello, world!" and use the str() constructor to convert it to a string object. 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.
Find elsewhere
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.7 documentation
Passing a bytes object to str() without the encoding or errors arguments falls under the first case of returning the informal string representation (see also the -b command-line option to Python).
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i convert a string to bytes?
r/learnpython on Reddit: How do I convert a string to bytes?
August 8, 2023 -

Suppose I something like

s = "GW\x25\001"

How do I convert that string to bytes, interpreting the backslashes as escapes? In other words, the resulting byte array should be of length 4.

UPDATE: Hmmm, I was taking the string from sys.argv[1], which seems to complicate things and not make it turn out as expected. So I'm still not sure what the answer is.

๐ŸŒ
Medium
medium.com โ€บ data-science โ€บ byte-string-unicode-string-raw-string-a-guide-to-all-strings-in-python-684c4c4960ba
Byte string, Unicode string, Raw string โ€” A Guide to all strings in Python | by Guangyuan(Frank) Li | TDS Archive | Medium
November 23, 2022 - As explained above, you can encode them to the byte string (b string), and the byte string can be decoded back to the Unicode string. u'Hi'.encode('ASCII') > b'Hi'b'\x48\x69'.decode('ASCII') > 'Hi' In Python IDE, usually, the byte string will be automatically decoded using โ€œASCIIโ€ when printed out, so thatโ€™s why the first result is human-readable (bโ€™Hi').
๐ŸŒ
w3resource
w3resource.com โ€บ python โ€บ python-bytes.php
Python Bytes, Bytearray
Bytes objects can be constructed the constructor, bytes(), and from literals; use a b prefix with normal string syntax: b'python'. To construct byte arrays, use the bytearray() function.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ convert-bytes-to-string-in-python
Convert Bytes to String in Python
November 27, 2020 - In this tutorial, we'll go over examples of how to convert bytes to a string in Python 2 and 3. We'll use the decode() function, str() function as well as 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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-bytes-to-string-in-python
How to Convert Bytes to String in Python ? - GeeksforGeeks
This method is used to convert from one encoding scheme, in which the argument string is encoded to the desired encoding scheme. This works opposite to the encode. ... It assumes the byte object is UTF-8 encoded unless specified otherwise. The str() function of Python returns the string version of the object.
Published: July 23, 2025
๐ŸŒ
Real Python
realpython.com โ€บ python-bytes
Bytes Objects: Handling Binary Data in Python โ€“ Real Python
March 5, 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.
๐ŸŒ
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).
๐ŸŒ
TakoVibe
takovibe.com โ€บ home โ€บ blog โ€บ python bytes vs string (str): differences, encoding, and when to use each
Python Bytes vs String (str): Differences, Encoding, and When to Use Each | Rahul Beniwal | TakoVibe
August 2, 2025 - In Python, str represents human-readable text (Unicode), while bytes represents raw binary data (8-bit values). Although they may look similar, mixing them incorrectly is one of the most common sources of bugs in Python.
๐ŸŒ
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.