Cannot use a string pattern on a bytes-like object in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# Cannot use a string pattern on a bytes-like object in Python

The Python "TypeError: cannot use a string pattern on a bytes-like object" occurs when we try to use a string pattern to match a bytes object.

To solve the error, use the decode() method to decode the bytes object, e.g. my_bytes.decode('utf-8').

typeerror cannot use a string pattern on a bytes like object

Here is an example of how the error occurs.

main.py
import re my_bytes = b'apple,banana,kiwi' # โ›”๏ธ TypeError: cannot use a string pattern on a bytes-like object m = re.search("apple", my_bytes)

We tried to use a string pattern to match a bytes object which is not supported.

# Decode the bytes object into a string

One way to solve the error is to decode the bytes object into a string.

main.py
import re my_bytes = b'apple,banana,kiwi' m = re.search("apple", my_bytes.decode('utf-8')) print(m) # ๐Ÿ‘‰๏ธ <re.Match object; span=(0, 5), match='apple'>

decode bytes object into string

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

Now we used a string pattern to find a match in a string, which is allowed.

# Using a bytes-pattern on a bytes-like object

Alternatively, you can use a bytes-pattern on a bytes-like object.

main.py
import re my_bytes = b'apple,banana,kiwi' # ๐Ÿ‘‡๏ธ notice b'' prefix m = re.search(b"apple", my_bytes) print(m) # ๐Ÿ‘‰๏ธ <re.Match object; span=(0, 5), match=b'apple'>

using bytes pattern on bytes like object

Notice that the first and the second arguments we passed to re.search are bytes objects.

Both of the arguments have to be of compatible types as mixing bytes objects and strings is not allowed.

We previously used the bytes.decode() method. However, if you need to convert a string to a bytes object, you have to use the str.encode() method.

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

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

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.

# Checking the type of a variable

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_str = 'hello' print(type(my_str)) # ๐Ÿ‘‰๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐Ÿ‘‰๏ธ True my_bytes = b'James Doe' print(type(my_bytes)) # ๐Ÿ‘‰๏ธ <class 'bytes'> print(isinstance(my_bytes, bytes)) # ๐Ÿ‘‰๏ธ True

checking the type of a variable

The type class returns the type of an object.

The isinstance() function returns True if the passed-in object is an instance or a subclass of the passed-in class.

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