How to Fix the Python Error: typeerror expected string or bytes like object

One of the best things about Python is its sheer versatility. The language can tackle a wide variety of specialized functions while still maintaining its ease of use. This is in large part due to the fact that it allows for considerable freedom in working with various data types. However, this versatility can make some error messages a little difficult to understand. For example, the “typeerror expected string or bytes like object” error often comes up when people start to work with regular expressions in Python. Thankfully, it’s also a fairly easy problem to fix.

What Does the Error Message Mean?


The “typeerror expected string or bytes like object” error refers to a conflict between an expected string data type and the data type actually in use. It’s most commonly seen when using regular expression matching or manipulation. Regular expressions, or regex, perform precise manipulation on string data. If it encounters another date type, such as an integer, it’ll typically fail with this or similar error messages.

A More In-Depth Examination of the Type Error

Python’s extreme versatility and ease of use is usually a good thing. However, there are instances where a combination of large scope and usability can bring up some issues. This is especially true in the context of data types and containers. Python is generally quite flexible in its use of variables. Python coders have a lot of freedom to mix variables of different types together without the conflicts found in many other languages. And this is especially true within Python containers.

Python containers often collect dissimilar data together into a single variable. It’s convenient, but it can lead to issues when trying to treat every item within the container in the same way. The classic example is a collection of mixed strings and numbers. When printed to screen these collections often look like the same data type. But in reality, they’re inherently incompatible with a lot of the functions they might be used with. This plays a role in the type error we’re looking at here. Thankfully this is generally fairly easy to fix. We can begin by recreating the error.

How To Fix the Type Error


import re
alpha = [‘b’, 999, 998, ‘c’, ‘d’, ‘e’, ‘ f’, ‘ G’, ‘888’ ]
alpha = re.sub(‘[^a-zA-Z]’, ”, alpha)
print(alpha)

This python code example attempts to use the regex sub function to remove everything but the letters of the alphabet from the list assigned to alpha. It begins by importing Python’s regular expression library. Next, we create a list of letters, numbers, and numbers in string format. This list is assigned to the alpha variable and passed to the sub function along with a regular expression. Anything outside of the accepted characters will be removed. Finally, the result of the sub function is assigned back to alpha and printed on the screen. If you run this code you’ll receive an error message of “typeerror expected string or bytes like object”.

The problem is subtle but significant. In the third line, we pass the alpha list to the sub function. However, sub expects strings and the alpha list contains both strings and integers. There’s an easy solution to this problem which you’ll see in the following example.

import re
alpha = [‘b’, 999, 998, ‘c’, ‘d’, ‘e’, ‘ f’, ‘ G_’, ‘888’ ]
alpha = re.sub(‘[^a-zA-Z]+’, ”, str(alpha))
print(alpha)

This code should run without any errors and return the expected characters as a string that is printed to the screen. The code is almost exactly the same as in the original example aside from a change to the way our alpha list is passed to sub. We now send the alpha list to the str function when calling sub. The str function converts everything within alpha into a string before passing the results on to sub. In doing so we bypass the issue of non-string characters causing an error within sub. Note that this also cleans up some of the formatting issues in alpha. Empty space, integers, and numbers in string format are all removed. Though we can retain the string formatted numbers and convert integers to strings in the following example.

import re
alpha = [‘b’, 999, 998, ‘c’, ‘d’, ‘e’, ‘ f’, ‘ G_’, ‘888’]
alpha = re.sub(r’\W+’, ”, str(alpha))
print(alpha)

This is similar to the previous example. The main difference is that it uses the regex shorthand of W to exclude numbers, letters, and underscores. This example demonstrates that we can work with numbers while still avoiding the previous error.

Both examples highlight the same solution to the python error. The str function gets around the larger problem with sub compatibility. And we even have options to format the results as needed by changing how the regex pattern sent to sub is formatted.

How to Fix the Python Error: typeerror expected string or bytes like object
Scroll to top