Last updated: Apr 9, 2024
Reading time·8 min
The Python "TypeError: 'in <string>' requires string as left operand, not
list" occurs when we use the in
operator with a left-hand side value of a list
and a right-hand side value of a string.
To solve the error, move the list to the right-hand side of the in
operator.
my_str = 'bobby' my_list = ['bobby', 'hadz', 'com'] # ⛔️ TypeError: 'in <string>' requires string as left operand, not list result = my_list in my_str
We used the in
operator with a list on the left-hand side and a string on the
right-hand side which caused the error.
in
operatorTo solve the error, move the list to the right-hand side of the in
operator.
# ✅ Checking if a value is in a list my_str = 'bobby' my_list = ['bobby', 'hadz', 'com'] result = my_str in my_list print(result) # 👉️ True if my_str in my_list: # 👇️ this runs print('The string is in the list') else: print('The string is NOT in the list')
The in operator tests
for membership. For example, x in l
evaluates to True
if x
is a member of
l
, otherwise it evaluates to False
.
my_list = ['bobby', 'hadz', 'com'] print('bobby' in my_list) # 👉️ True print('another' in my_list) # 👉️ False
If you need to check if a string is NOT in a list, use the not in
operator
instead.
my_list = ['bobby', 'hadz', 'com'] print('bobby' not in my_list) # 👉️ False print('another' not in my_list) # 👉️ True # --------------------------------------- if 'another' not in my_list: # 👇️ This runs print('The value is NOT in the List') else: print('The value is in the List')
x not in l
returns the negation of x in l
.
In other words, the not in
operator will return True
if the value is not in
the list and False
otherwise.
in
and not in
operators.The in
operator can also be used to check if a string contains a substring.
a_str = 'bobbyhadz.com' substring = '.com' if substring in a_str: # 👇️ This runs print('The substring is contained in the string') else: print('The substring is NOT contained in the string')
When used with two strings, the in
operator returns True
if the substring is
contained in the string and False
otherwise.
Note that the value on the left-hand side has to be of type string.
If the value is an integer or any other type, you have to use the str()
class
to convert it to a string to be able to use the in
operator.
a_str = 'bobbyhadz.com123456' an_int = 123 # ✅ Convert integer to string if str(an_int) in a_str: # 👇️ This runs print('The substring is contained in the string') else: print('The substring is NOT contained in the string')
We used the str()
class to convert the integer to a string to be able to use
the in
operator.
If you need to check if a string is contained in a list in a case-insensitive manner, convert the string and the list's items to lowercase.
my_list = ['bobby', 'hadz', 'com'] my_str = 'BOBBY' if my_str.lower() in (word.lower() for word in my_list): # 👇️ This runs print('The string is contained in the list') else: print('The string is not contained in the list')
The expression will return True
if the string is contained in the list in a
case-insensitive manner, otherwise, False
is returned.
If you need to check if a substring is contained in a list, use the any()
function.
my_list = ['bobby123', 'hadz456', 'com789'] my_str = 'bobby' if any(my_str in item for item in my_list): # 👇️ This runs print('The substring is contained in the list') else: print('The substring is NOT contained in the list') # ----------------------------------------------- print(any('bobby' in item for item in my_list)) # 👉️ True print(any('another' in item for item in my_list)) # 👉️ False
The any()
function will return True
if the substring is contained in a list
and False
otherwise.
You can also use this approach to check if a list contains another list, if you have a two-dimensional list.
my_list = [['bobby'], ['hadz'], ['com']] print(['hadz'] in my_list) # 👉️ True print(['bobby'] in my_list) # 👉️ True print(['another'] in my_list) # 👉️ False
The expression returns True
if the given sublist is contained in the list and
False
otherwise.
The Python "TypeError: 'in <string>' requires string as left operand, not
int" occurs when we use the in
operator with an integer and a string.
To solve the error, wrap the integer in quotes, e.g. '5' in my_str
.
Here is an example of how the error occurs.
my_str = '13579' result = 13 in my_str # ⛔️ TypeError: 'in <string>' requires string as left operand, not int print(result)
We used the in
operator to check if an integer is contained in a string which
caused the error.
in
operatorTo solve the error, we have to wrap the integer in quotes to make it a string.
my_str = '13579' result = '13' in my_str print(result) # 👉️ True if '13' in my_str: # 👇️ This runs print('13 is contained in the string') else: print('13 is NOT contained in the string')
str()
class to convert the integer to a stringIf you have the int stored in
a variable, use the str()
class to convert it to a string before using the
in
operator.
my_str = '13579' my_int = 13 result = str(my_int) in my_str print(result) # 👉️ True if str(my_int) in my_str: # 👇️ This runs print('13 is contained in the string') else: print('13 is NOT contained in the string')
The str()
class can be used to convert an object (such as an int
) to a
string.
Once the values on the left-hand and right-hand sides are of the same type, the error is resolved.
The in operator tests
for membership. For example, x in s
evaluates to True
if x
is a member of
s
, otherwise it evaluates to False
.
my_str = 'num is 13579' print('5' in my_str) # 👉️ True print('0' in my_str) # 👉️ False
The in
operator performs a case-insensitive test for whether the substring is
contained in the string.
If you need to ignore the case, convert both strings to lowercase.
a_str = 'BOBBYHADZ.COM' substring = 'com' if substring.lower() in a_str.lower(): # 👇️ This runs print('The substring is contained in the string') else: print('The substring is NOT contained in the string')
The str.lower method returns a copy of the string with all the cased characters converted to lowercase.
Converting both strings to the same case allows for a case-insensitive membership test.
If you need to check if a substring is NOT in a string, use the not in
operator instead.
my_str = 'num is 13579' print('5' not in my_str) # 👉️ False print('0' not in my_str) # 👉️ True
x not in s
returns the negation of x in s
.
in
and not in
operators.Note that empty strings are always considered to be a substring of any other string.
my_str = '13579' print('' in my_str) # 👉️ True
Checking if an empty string is a substring of any other string will return
True
.
The "TypeError: 'in <string>' requires string as left operand, not dict" error
is raised when you use the in
operator with a dictionary as the left-hand side
value.
To solve the error, specify the dictionary on the right-hand side when checking if a key exists.
my_dict = {'name': 'Bobby Hadz', 'age': 30} # ⛔️ TypeError: 'in <string>' requires string as left operand, not dict if my_dict in 'name': print('The key is contained in the dict')
The code sample causes the error because the dictionary is on the left-hand side
of the in
operator.
To solve the error, move the dictionary to the right of the operator.
my_dict = {'name': 'Bobby Hadz', 'age': 30} if 'name' in my_dict: print('The key is contained in the dict') else: print('The key is NOT contained in the dict')
in
operator checks for the existence of the specified key in the dict
object.The in
operator will return True
if the key exists in the dictionary and
False
otherwise.
my_dict = {'name': 'Bobby Hadz', 'age': 30} print('name' in my_dict) # 👉️ True print('another' in my_dict) # 👉️ False
If you need to check if a key is not in a dictionary, use the not in
operator.
my_dict = {'name': 'Bobby Hadz', 'age': 30} if 'another' not in my_dict: # 👇️ This runs print('The key is NOT contained in the dict') else: print('The key is contained in the dict') # ---------------------------------------- print('name' not in my_dict) # 👉️ False print('another' not in my_dict) # 👉️ True
The not in
operator will return True
if the key is not contained in the
dictionary and False
otherwise.
You can learn more about the related topics by checking out the following tutorials: