Loop Through List of Strings in Python (3 Examples)
In this tutorial, you’ll learn how to loop through a list of strings in Python and perform operations on each element within the list. We’ll explore different examples to demonstrate this concept.
The table of contents is structured as follows:
Let’s dive into it!
Creating Sample Data
Firstly let’s initialize a sample string list,
names = ["Alice", "Bob", "Charlie", "Dave", "Eve"]
Now let’s get to the examples.
Example 1: Looping Through a List
The first example involves looping through a list of strings using a for loop. Here’s how it works:
for name in names: print(name) # Alice # Bob # Charlie # Dave # Eve
In this example, we have a list of strings called names
. The for
loop iterates over each element in the list, and we print the values of name
.
Example 2: Looping Through a List with Index
The second example demonstrates how to loop through a list of strings and access both the value and index of each element. Here’s an example:
for index, name in enumerate(names): print(f"Index: {index}, Value: {name}") # Index: 0, Value: Alice # Index: 1, Value: Bob # Index: 2, Value: Charlie # Index: 3, Value: Dave # Index: 4, Value: Eve
In this example, we use the enumerate() function to get both the index and value of each element in the list. The loop iterates over the list, and for each iteration, we print the index and value.
Example 3: Looping Through a List with Conditional Statements
The third example demonstrates how to loop through a list of strings and perform operations based on apply conditional statements. Here’s an example:
for name in names: if len(name) > 4: print(f"{name} has more than 4 characters") else: print(f"{name} has 4 or fewer characters") # Alice has 4 or fewer characters # Bob has 4 or fewer characters # Charlie has more than 4 characters # Dave has 4 or fewer characters # Eve has 4 or fewer characters
In this example, we loop through the list of strings. We use an if
statement to check the length of each string and print a corresponding message based on the condition.
Video, Further Resources & Summary
Do you need more explanations on looping through a list of strings in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Convert List from Character String to Integer in Python
- Lists in Python (13 Examples)
- Loop Over Range List in Python
- Loop Through List of Integers in Python
- Iterate Over Nested Dictionary with List in Python
This post has shown how to loop through a list of strings in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter