TypeError: expected string or bytes-like object in Python

In Python, different modules and open-source libraries provide various functions that are used to perform multiple operations. Sometimes, while dealing with a function, if the user inputs an integer or string value as an argument, the error “TypeError: expected string or bytes-like object” occurs.

Ths Python write-up will present the causes and solutions of “TypeError: expected string or bytes-like object”. The following points are discussed in this Python tutorial:

Reason 1: Passing Unexpected Argument Value to String Method

When the user passes different data type values to a method that accepts only string type values.

The above snippet shows an error because the “re.sub()” method accepts only string value as an argument. But we pass “integer” as an argument value to a “re.sub()” method.

Solution 1: Use the str() Function to Convert it into a String

To resolve this “TypeError”, the “str()” function is utilized to convert the integer into strings. This is because the “re.sub()” method only accepts the string as an argument. The example code shown below will show you how to resolve the above error:

Code:

import re

value = 12

output = re.sub(r'[0-9]', 'ab', str(value))
print(output)

In the above code:

  • The “re” module is imported at the start to access the function “re.sub()” in the program.
  • The “re.sub()” accepts three parameter values; the first parameter, “[(0-9)]” takes the pattern value that needs to be replaced.
  • The second parameter, “ab” specifies the value that will replace the pattern. While the third parameter, “value” is necessary as it accepts the string that the replacement operation using “re.sub()” has to perform.
  • The “str()” function is utilized to convert the third argument value into a string that will solve our “TypeError”.

Output:

The above snippet shows that the substring value “1” and “2” has been successfully replaced by “ab”.

Solution 2: Provide an Empty String

If the input string value in which the “re.sub()” function performed a replacement operation is “None” then provide an empty string. The empty string will resolve this “TypeError” while executing the program.

Code:

import re

value = None

output = re.sub(r'[0-9]', 'ab', value or '')
print(output)

In the above code:

  • The logical “or” operator is used between the “none” variable and the “empty” string.
  • The “re.sub()” method evaluates the empty string because this method always takes the string or byte-like object as an argument.

Note: We can provide any string in place of “empty string”. The “re.sub()” returns the new string by replacing the occurrences of that provided string.

Output:

The above output shows an empty string because the “re.sub()” method expects a string as an argument rather than any other data type.

Reason 2: Using Function That Returns Other Than String

The error occurs when a user uses a function that returns a value other than strings, such as the “readlines()” function. The “readlines()” function returns “list” as an output. As we know, the “re.findall()” returns the “expected string or bytes-like object” error in Python when the string is not passed as an argument.

The above snippet shows the error “expected string or bytes-like object” because the “list” is used as an argument of the “re.findall()” method.

Solution: Use Function That Returns String

To resolve this error, use a function that returns string value rather than other data types. Because the “re.findall()” method only accepts the string value as a parameter. The “read()” function is used in place of “readlines()” to return string values.

Code:

import re

with open('sample.txt', 'r') as f:
    value = f.read()

    output = re.findall(r'\w+on', value)

print(output)

In the above code, the “read()” function reads the file “sample.txt” and returns the value in a string. Then the “re.findall()” method accepts the value and finds the specified character from the given string without any error.

Output:

The above output returns the value from the file that ends with the “on” character.

Conclusion

The “expected string or bytes-like object” error arises when we pass various data type values to a method that expects string type parameter values. To resolve this error, various solutions are used in Python, such as using the “str()” function, providing the empty string, and using a function that returns a string type value. This article presented various reasons and solutions for the “expected string or bytes-like object” errors in Python.