5 Easy Ways to Find String in List in Python

We got to know about many topics in python. But have you ever tried to find the string in a list in Python? In this tutorial, we will be focusing on finding the string in a list. There are multiple ways through which we can find the string in a list. We will be discussing all of them in this article.

We will find the string in a python list with multiple ways like for loop, in operator, using count, any() function.

5 Ways With Examples to Find The String In List in Python

We will discuss how we can find the string in a list with examples explained in detail.

1. Using Simple For Loop

In this example, we will be making a list with elements of a string. We will then take an input string from the user, which we want to find that the string contains in a list. By using for loop, we will find the string in a list. Let us look in detail with the help of the example explained below:

#input list
lst = ['abc', 'def', 'ghi','python', 'pool']

#input string from user
str = input("Enter the input string : ")
c = 0
#applying loop
for i in lst:
    if i == str:
        c = 1
        break
if c == 1:
    print("String is present")
else:
    print("String is not present")

Output:

Enter the input string : python
String is present

Explanation:

  • Firstly, we will make a list that will contain some element in the form of a string.
  • Then, we will take the input from the user in the form of a string.
  • We will take a variable c which is set to 0.
  • After that, we will be applying for loop.
  • Inside for loop, we will apply if condition in which we will check if the element of the list is equal to the string. If they are equal c value becomes 1, and the loop gets breaks.
  • At last, we will check if c is equal to 1 or 0. if c is equal to 1, we will print string is present and if c == 0, we will print string is not present.
  • Hence, you can see the output.

2. Using in operator to Find The String List in Python

It is the membership operator in python. It is used to test whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).

In this example, we will be making a list with elements of a string. We will apply in operator if we give the string to be searched and check if it is in the list. Let us look in detail with the help of the example explained below:

#input list
lst = ['abc', 'def', 'ghi','python', 'pool']

#applying if condition
if 'pool' in lst:
    print("string is present")
else:
    print("string is not present")

#checking false condition
if 'pools' in lst:
    print("string is present")
else:
    print("string is not present")

Output:

string is present
string is not present

Explanation:

  • Firstly, we will make a list that will contain some element in the form of a string.
  • Then, we will apply the ‘in’ operator inside the if condition and check if the string we want to check is present in the given list or not.
  • If the string is present, the output is printed as the string is present, and if the string is not present, the output is printed as the string is not present.
  • We have taken both examples. One of the lists is present, and one of the lists is not present in the program.
  • Hence, you can see the output.

3. Using count() function

The count() function is used to count the occurrences of a string in a list. If we get the output as 0, then it says that the string does not contain the list, and if the output is 1, then the string is present in the list.

In this example, we will be making a list with elements of a string. We will then take an input string from the user, which we want to find that the string contains in a list. Then, we will apply the count function passing the input string and see the count value through the if condition, and print the output.

#input list
lst = ['abc', 'def', 'ghi','python', 'pool']

#input string from user
str = input("Enter the input string : ")

#count function
count = lst.count(str)

if count > 0:
    print("String is present")
else:
    print("String is not present")

Output:

Enter the input string : ghi
String is present

Explanation:

  • Firstly, we will make a list that will contain some element in the form of a string.
  • Then, we will take the input from the user in the form of a string.
  • Then, we will apply the count() function, which will count the string’s occurrence in a list.
  • If the count’s value is equal to 0, the output will be printed as the string is not present, and if the count is greater than 0, the output is printed as the string is present in the list.
  • Hence, you can see the output.

4. Using any() function to Find String In List in Python

The any() function returns True if the string is present in a list. Otherwise, it returns False. If the list is empty, it will return False.

In this example, we will be making a list with elements of a string. We will apply if condition, and in that, we will apply any() function through which we can find the string in a list is present or not.

#input list
lst = ['abc', 'def', 'ghi','python', 'pool']
 
if any("pool" in word for word in lst):
    print("string is present")
else:
    print("string is not present")

Output:

string is present

Explanation:

  • Firstly, we will make a list that will contain some element in the form of a string.
  • Then, we will apply any() function inside the condition where we will give the string we have to find and apply for loop.
  • If the function finds any same string as given, the output gets printed as the string is present, and if it does not find any such string, the output gets printed as a string is not present.
  • Hence, you can finally see the output as the string is present as the pool is present in the list.

5. Finding all the indexes of a particular string

In this example, we will be making a list with elements of a string. Then, we will be selecting a particular string and try to find all the indexes at which the string is present. We will be using len() function for finding the length of the list and while loop for running the loop from starting till the end of the list.

#input list
lst = ['A','D', 'B', 'C', 'D', 'A', 'A', 'C', 'D']
s = 'D'
index = []
i = 0
length = len(lst)

while i < length:
    if s == lst[i]:
        index.append(i)
    i += 1
print(f'{s} is present in {index}')

Output:

D is present in [1, 4, 8]

Explanation:

  • Firstly, we will make a list that will contain some element in the form of a string.
  • Then, We will take a particular string of which we want to find the present indexes.
  • After that, we will make an empty list as index and keep i=0.
  • Then, we will calculate the length of the list.
  • After that, we will apply while loop till the length of the list and check the condition if the string is preset at that index of list or not.
  • If present we will store the index value in the new list index and continue the loop till the end.
  • At last, we will print all the indexes.

Conclusion

In this tutorial, we have learned about how to find the string in a list. We have seen all the multiple methods through which we can find the string present in the list. We have also discussed all the methods with the examples explained in detail. You can use any of the methods according to your need in the program.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments