Last modified: May 14, 2023 By Alexander Williams

How to Solve startswith first arg must be str or a tuple of str, not [bool-list-int]

The TypeError error is raised when we pass an incompatible object for an operation.

For example:

print(True + 11)

Here, we will get TypeError because we cannot concatenate a bool with a number. And the same thing happens when we pass an incompatible object for the startswith() function.

However, in this tutorial, we will cover the reasons for TypeError: startswith first arg must be str or a tuple of str, not (bool/list/int) and how to solve it.

What are the reasons for 'startswith first arg must be str or a tuple of str'?

If we read the TypeError: startswith first arg must be str or a tuple of str, not (bool/list/int) error carefully, we will see that startswith accepts either string or tuple of string in the first argument. Therefore, when we pass something else, we will get the error.

For example:

# Passing Bool
"my string".startswith(True)
#TypeError: startswith first arg must be str or a tuple of str, not bool


# Passing List
"my string".startswith(['python', 'hello', 'go'])
#TypeError: startswith first arg must be str or a tuple of str, not list


# Passing Integer
"my string".startswith(1)
#TypeError: startswith first arg must be str or a tuple of str, not int

Next, we will see the solutions of each of the examples above.

How to solve 'startswith first arg must be str or a tuple of str'

To solve the error, let us see the solutions of the previous examples.

1. Passing Bool

Example:

# Passing Bool
"my string".startswith(True)
#TypeError: startswith first arg must be str or a tuple of str, not bool

In this situation, we need to convert the bool type to string by using the [single/double] quote or the str() function.

Double quote:

my_string = "Hello Error"
print(my_string.startswith("True"))

Output:

False

 

str():

 

my_string = "Hello Error"
print(my_string.startswith(str(True)))

Output:

False

2. Passing List

# Passing List
"my string".startswith(['python', 'hello', 'go'])
#TypeError: startswith first arg must be str or a tuple of str, not list

Here, we have two solutions:

  • Convert the list to a tuple
  • Use the any() function

Convert the list to a tuple:

my_string = "Hello Error"
prefixes = ['python', 'Hello', 'go']
print(my_string.startswith(tuple(prefixes)))

Output:

True

The tuple() function converts a list to a tuple.

 

Use any():

 

my_string = "Hello Error"
prefixes = ['python', 'Hello', 'go']
print(any(my_string.startswith(p) for p in prefixes))

Output:

True

Click Here for more information about Check if String Starts with list of prefixes.

3. Passing Integer

# Passing Integer
"my string".startswith(1)
#TypeError: startswith first arg must be str or a tuple of str, not int

To solve int type error, we will do like the bool example.

Double quote:

my_string = "Hello Error"
print(my_string.startswith("1"))

Output:

False

 

str():

 

my_string = "Hello Error"
print(my_string.startswith(str(1)))

Output:

False

Conclusion

Here we have learned the solutions for 'startswith first arg must be str or a tuple of str, not [bool-list-int]'.

In general, when we face TypeError, the first thing we have to think about is the type of object.

We hope this tutorial helps you to solve the problem.