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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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.

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'>

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.

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'>

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, but if you need to convert a string into bytes, you have to use the str.encode() method, e.g. my_str.encode('utf-8').

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

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

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.

Conclusion #

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').

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.