find all letter 'e' in a word in python

To find all occurrences of the letter 'e' in a given word, you can use a for loop in Python to iterate through each character of the word and check if it is equal to 'e'. You can also use the built-in count() method of the string class to count the number of occurrences of 'e' in the word. Here's an example code snippet:

main.py
word = "example"

# Method 1: Using a for loop to find all occurrences of 'e'
e_indices = []
for i in range(len(word)):
    if word[i] == 'e':
        e_indices.append(i)
print("Method 1: e_indices =", e_indices)

# Method 2: Using the count() method to count the number of occurrences of 'e'
e_count = word.count('e')
print("Method 2: e_count =", e_count)
357 chars
13 lines

Output:

main.py
Method 1: e_indices = [0, 6]
Method 2: e_count = 2
51 chars
3 lines

In Method 1, we create an empty list e_indices to store the indices of all occurrences of 'e' in the word. We then iterate through each character of the word using a for loop and check if it is equal to 'e'. If it is, we append the index of that character to the e_indices list. Finally, we print the e_indices list to show the indices of all occurrences of 'e' in the word.

In Method 2, we simply use the count() method of the string class to count the number of occurrences of 'e' in the word. We then print the count to show how many times 'e' appears in the word.

gistlibby LogSnag