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.py357 chars13 lines
Output:
main.py51 chars3 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