Find String in List in Python (3 Examples)

 

Hi! This tutorial will show you how to get a string in lists in the Python programming language.

Here is an overview:

Let’s get right into the Python code!

 

Create Example Python List of String Values

Here, we will create the example Python list of string values that will be used in the examples in this tutorial. So, in your Python programming IDE, run the code below to create the example Python list of strings:

my_list = ["car", "radio", "wheels", "bicycle"]
 
print(my_list)
# ['car', 'radio', 'wheels', 'bicycle']
 
print(type(my_list))
# <class 'list'>

Now that we have created the example list of strings, we will examine ways of determining if the search string is present in the list. The search string is defined below:

search_string = "radio"

Let’s now find it in my_list!

 

Example 1: Get String in List Using in Operator & Conditional if Statement

In this first example, we will use the in operator and the conditional if statement to determine if the search string exists in the list:

if search_string in my_list:
  print(True)
else:
  print(False)
 
# True

The above example checks if the variable search_string is present in the list my_list. It uses the in operator, which returns the boolean True if the search string is found in the list and False otherwise.

The True value is printed in our case since we have the search string in my_list.

Example 2: Get String in List Using for Loop

In this next example, we will use a for loop to check for the search string in the list:

for item in my_list:
  if item == search_string:
    print(True)
    break
 
# True

Here, a for loop is used to iterate through each item in the list my_list. Within the loop, it checks if the current item is equal to the search_string using the == operator.

If there is a match, meaning the search_string is found in the list, the print(True) statement is executed, indicating that the search string was found.

Additionally, it includes a break statement, which terminates the loop as soon as a match is found. This ensures that only the first occurrence of the search_string is considered, and the loop doesn’t continue unnecessarily.

 

Example 3: Get String in List Using index() Method

In this last example, we will use the index() method to get the search string in the list:

try:
  my_list.index(search_string)
  print(True)
except ValueError:
  print(False)
 
# True

This example attempts to find the index of search_string within my_list using the index() method. The try block encloses this method and expects it to run without any errors.

If the search_string is found in the list, the index() method will return its index. In this case, the print(True) statement is executed, indicating that the search string was found.

However, if the search_string is not present in the list, the index() method will raise a ValueError. The except block catches this specific error and executes the print(False) statement, indicating that the search string was not found.

 

So, with that, we have demonstrated 3 easy ways to find strings in lists in Python. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to inspect a string in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to search for a string in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find strings in a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top