How To Resolve “TypeError: ‘In ‘ Requires String As Left Operand, Not Int”

How To Resolve TypeError: ‘in ‘ Requires String As Left Operand, Not Int In Python

To fix the “TypeError: ‘in <string>’ requires string as left operand, not int” error in Python, we can use the str() function, put integers in brackets ‘ ‘ and the error will be resolved. Follow the article to better understand.

What causes the error “TypeError: ‘in <string>’ requires string as left operand, not int”?

The mining operator (in and not in) checks the value of an iterable object. Iterable objects such as lists, tuples, and dictionaries must be validated.

  • The ”in” operator returns True if the object’s value before ”in” is found in the object after the ”in” operator and vice versa.
  • The ”not in” operator returns True if no value of the object before ”in” is found in the object after the ”not in” operator and vice versa.

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type. 

In short, the error “TypeError: ‘in <string>’ requires string as left operand, not int” happens when you use the member operator to compare a string with an integer.

Example: 

myStr = 'visit learnshareit website'
myNumber = 2003

print(myNumber in myStr)

Output:

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    print(myNumber in myStr)
TypeError: 'in <string>' requires string as left operand, not int

I have a string and an integer. The problem here is I used the ‘in’ operator to compare the string ‘myStr’ with the number 2003 and got an error like the above.

How to solve this error?

Use the str() function

You can use the str() function to convert integers to strings. This way will solve the problem.

Syntax:

str(object)

Parameters:

  • object: is an object that can be displayed as a string.

The str() function returns a string.

Example:

myStr = 'visit learnshareit website'
myNumber = 2003

# Use the str() function to convert myNumber to string form
newStr = str(myNumber)
print(myStr in newStr)

Output:

False

In the above example, I create a string, declare a variable whose value is an integer, and use the ‘in’ operator to check. The characters of string ”myStr” does not appear in string ‘newStr’, so return the value ”False”.

Put integer in brackets ‘ ‘

Here also is a solution for you. This is not too different for you to convert an integer to a string using the str() function.

Example:

firstStr = 'visit learnshareit website'
secondStr = '2003'  
print(firstStr in secondStr)

Output:

False

In the above example, I create a string and declare a variable whose value is an integer. Enclosing the integer in brackets ‘ ‘ has the same effect as converting it to a string. Use the ‘in’ operator to check. The characters of the string ‘firstStr‘ does not appear in string ‘secondStr‘, so return the value ”False”.

Note: you can use the ‘not in’ operator to check. The return I have mentioned above is the cause of the error.

Summary

If you have any questions about the “TypeError: ‘in <string>’ requires string as left operand, not int” in Python, leave a comment below. I will answer your questions.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *