I am doing an exercise where for a string, you print the index of all of its vowels. I was able to do it with enumerate() but still don't understand why the below did not work:
s = "And now for something completely different"
vowels = ["A", "a", "E", "e", "I", "i", "O", "o", "U", "u"]
for char in s:
if char in vowels:
print(s.index(char))
else:
continue
This outputs: 0 5 5 5 15 18 5 15 15 18 15 15.
In addition, I noticed that it still outputs the same without the "else" statement: does this mean that I don't explicitly need to state (with "continue") to try the next character if the current one is not in the input string?
What I wanted to achieve can be done as follows:
s = "And now for something completely different"
vowels = ["A", "a", "E", "e", "I", "i", "O", "o", "U", "u"]
for i, char in enumerate(s):
if char in vowels:
print(i)
This correctly outputs: 0 5 9 13 15 18 23 27 29 34 37 39.