0

I need to print out the vowels in horton in the order they appear using a for loop, this is what i have so far.

horton = "A person's a person, no matter how small."
vowels = "aeiouAEIOU" 
for letters in horton:
if letters == vowels[0:9]:
    print(letters)
Casey Hnasko
  • 1
  • 1
  • 2

2 Answers2

5

Welcome to StackOverflow!

Replace if letters == vowels[0:9]: to if letters in vowels: will solve your problem.

Simple explanation: == will check whether the left element is identical to the right element, in your case, to the left of == is a single letter, while to the right is "aeiouAEIO" (yeah, there's a capital U missing as well) and they can't be identical in any case.

Full programme:

horton = "A person's a person, no matter how small."
vowels = "aeiouAEIOU" 
for letters in horton:
    if letters in vowels:
        print(letters)
# A
# e
# o
# a
# e
# o
# o
# a
# e
# o
# a
Kevin Fang
  • 1,966
  • 2
  • 16
  • 31
0

One-liner:

print(''.join([i for i in horton if i.lower() in 'aeiou']))

Output:

Aeoaeooaeoa

If want in newlines:

print('\n'.join([i for i in horton if i.lower() in 'aeiou'])) 

Output:

A
e
o
a
e
o
o
a
e
o
a

Or regex:

import re
print(''.join(re.findall(r'[aeiou]',"mississippi")))

Output:

Aeoaeooaeoa

If newline:

import re
print('\n'.join(re.findall(r'[aeiou]',"mississippi")))

Output:

A
e
o
a
e
o
o
a
e
o
a
U13-Forward
  • 69,221
  • 14
  • 89
  • 114