In: Computer Science
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer
code Images
Typed Code:
# A function called contains_string with input parameters
# List_strings is a List of strings
# string is a word(string)
def contains_string(List_strings ,string):
# traversing through all the elements of the List
for x in List_strings:
# if x is same as string
if(x==string):
# returning True
return True
# if string is not in List_strings
# then returning False
return False
# A List of strings
List_strings = ["eggs", "milk", "bananas"]
# A variable string with value pumpkin
string = "pumpkin"
# calling the function contains_string
# printing the returned value
print("List of strings contains ",string,":
",contains_string(List_strings,string))
#code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!