Python Forum

Full Version: How to print only vowels from an input?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,
I am running into a task that needs your help.
The task is:
Ask for user input, and write a for loop that will output all the vowels within it. For example:
>>> "Hello" ➔ "eo"

My code is here:

word = input("Write your word here: ")
print(word)

for letter in word:
    if letter == 'aeiou':
        print(letter)
I really appreciate your help!
Thank you.
i found the solution
my code:
word = input("Write your word here: ")
print(word)

for letter in word:
	if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
		print(letter)
as you can see here i added alot of ors, which basically in human will be like this:
hey python, if the letter variable equals to string a, or the letter variable equals to string b, or the letter variable equals to string i, or the letter variable equals to string o, or the letter variable equals to string u, then print the letter variable
Neither of your solutions match the example given in the task and what about upper case letters:
Output:
"All the happy people" -> "Aeaeoe"
It is a quite simple beginners problem that you ought to be able to solve after studying pythons print command e.g. read about print() on RealPython
I don't like printing a letter at a time and I want more control over formatting.
word = input('Enter a word ')
print(f"{word} -> {''.join([a for a in word if a in 'aeiouAEIOU'])}")
@deanhystad I also like to collect everything prior to printing (or whatever) and I also like good control over formatting but in a beginners context where one has come to the point where for loops have just been introduced I think that this is a slightly more plausible approach:

txt = input('Write something: ') # No mention of a single word in the task
print(f'"{txt}" -> "', end = '')
for c in txt:
    #<continue printing if c is a wowel>
print('"')
Where substituting code for the last comment is a nobrainer if they study both our examples, and it meets exactly the example in the task text.
(Feb-25-2021, 10:13 PM)NullAdmin Wrote: [ -> ]
word = input("Write your word here: ")
print(word)

for letter in word:
	if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u':
		print(letter)
as you can see here i added alot of ors

I would suggest to replace row #5 with following:

if letter.lower() in 'aeiou':
This will get rid of repeating or's and also catches uppercase vowels.
A combination of "".join() and a generator expression with conditional.
The conditional does containment checking for each char. Before the comparison is made, the char is made lower case.


VOWELS = "aeiou"
text = input("Enter your text: ")

only_vowels = "".join(char for char in text if char.lower() in VOWELS)
no_vowels = "".join(char for char in text if char.lower() not in VOWELS)
I don't use join very often (hardly at all) and was unaware that you can use a condition in the iterator. Good to know.

And that is why I used join and the list comprehension in my reply. To only answer the question the answer provides the least amount of information and the questioner is left with the most amount of ignorance. Yes, you can print out a letter at a time using additional arguments in the print statement, but nobody would write the solution that way. Yes you can specify separators and ends and all kinds of things in the print statement, but almost everyone uses some sort of formatter instead.

Now PP904 knows that you can change the end character for a print statement to be a space or a blank instead of a newline, that there is a way to join multiple characters into a string, and that you can specify output format when printing. Just like I now know that I can control what is joined when using the join() string method.
(Feb-26-2021, 03:08 PM)deanhystad Wrote: [ -> ]I don't use join very often (hardly at all) and was unaware that you can use a condition in the iterator. Good to know.

And that is why I used join and the list comprehension in my reply. To only answer the question the answer provides the least amount of information and the questioner is left with the most amount of ignorance. Yes, you can print out a letter at a time using additional arguments in the print statement, but nobody would write the solution that way. Yes you can specify separators and ends and all kinds of things in the print statement, but almost everyone uses some sort of formatter instead.

Now PP904 knows that you can change the end character for a print statement to be a space or a blank instead of a newline, that there is a way to join multiple characters into a string, and that you can specify output format when printing. Just like I now know that I can control what is joined when using the join() string method.
Sorry to have hurt your feelings. I simply want us, who supposedly are experienced programmers (and maybe teachers), to try to understand the students situation and programming skill level and try to adapt to it. Sometimes solutions given on this site are far above the apparent level of the person posting the question, which I think is more bewildering than helpful. I will however be more careful in my comments from now on.