How to remove the 'b' prefix from a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Remove the 'b' prefix from a string in Python
  2. Remove the 'b' prefix from a string using str()
  3. Remove the 'b' prefix from a string using repr()

# Remove the 'b' prefix from a string in Python

Use the bytes.decode() method to remove the b prefix from a bytes object by converting it to a string.

The decode() method will return a string decoded from the given bytes object and will remove the b prefix.

main.py
my_bytes = 'bobbyhadz.com'.encode('utf-8') print(my_bytes) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' print(type(my_bytes)) # ๐Ÿ‘‰๏ธ <class 'bytes'> string = my_bytes.decode('utf-8') print(string) # ๐Ÿ‘‰๏ธ bobbyhadz.com print(type(string)) # ๐Ÿ‘‰๏ธ <class 'str'>

remove b prefix from string

The code for this article is available on GitHub

Bytes objects are always prefixed with b'', so to remove the prefix, we have to convert the bytes to a string.

The bytes.decode() method returns a string decoded from the given bytes. The default encoding is utf-8.

Conversely, the str.encode() method returns an encoded version of the string as a bytes object. The default encoding is utf-8.

main.py
my_bytes = 'bobbyhadz.com'.encode('utf-8') print(my_bytes) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' string = my_bytes.decode('utf-8') print(string) # ๐Ÿ‘‰๏ธ bobbyhadz.com

using str encode and bytes decode

Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

In other words, you can use the str.encode() method to go from str to bytes and bytes.decode() to go from bytes to str.

# Remove the 'b' prefix from a string using str()

Alternatively, you can use the str() class.

main.py
my_bytes = bytes('bobbyhadz.com', encoding='utf-8') print(my_bytes) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' print(type(my_bytes)) # ๐Ÿ‘‰๏ธ <class 'bytes'> string = str(my_bytes, encoding='utf-8') print(string) # ๐Ÿ‘‰๏ธ bobbyhadz.com print(type(string)) # ๐Ÿ‘‰๏ธ <class 'str'>

remove b prefix using str class

The code for this article is available on GitHub

The str() class returns a string version of the given object. If an object is not provided, the class returns an empty string.

The syntax for using the bytes() class is the same. The class returns a bytes object, so the b prefix is added.

We used the utf-8 encoding in the examples. The utf-8 encoding is capable of encoding over a million valid character code points in Unicode.

You can view all of the standard encodings in this table of the official docs.

Some of the common encodings are ascii, latin-1 and utf-32.

When decoding a bytes object, we have to use the same encoding that was used to encode the string to a bytes object.

# Remove the 'b' prefix from a string using repr()

If for some reason, you need to remove the b prefix in a hacky way, try using the repr() function with string slicing.

main.py
my_bytes = bytes('bobbyhadz.com', encoding='utf-8') print(my_bytes) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' string = repr(my_bytes)[2:-1] print(string) # ๐Ÿ‘‰๏ธ bobbyhadz.com

remove b prefix from string using repr

The code for this article is available on GitHub

The repr() function returns the canonical string representation of the object.

main.py
my_bytes = bytes('bobbyhadz.com', encoding='utf-8') print(my_bytes) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' print(repr(my_bytes)) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com'

Once we have the bytes object converted to a string, we can use string-slicing to remove the b prefix.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[2:-1] starts at index 2 and goes up to, but not including the last character in the string.

We basically exclude the b character and the quotes.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev