Python Forum
How to print only vowels from an input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print only vowels from an input?
#1
Bug 
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.
buran write Feb-26-2021, 12:46 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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
PP9044 likes this post
Reply
#3
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
PP9044 likes this post
Reply
#4
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'])}")
Reply
#5
@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.
Reply
#6
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
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.
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print user input into triangle djtjhokie 1 2,390 Nov-07-2020, 07:01 PM
Last Post: buran
  Counting Vowels in Python sapphosvoice 1 3,314 May-05-2020, 04:24 AM
Last Post: buran
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,558 Feb-27-2020, 09:22 AM
Last Post: buran
  Print the longest str from user input edwdas 5 4,174 Nov-04-2019, 02:02 PM
Last Post: perfringo
  Return not vowels list erfanakbari1 2 2,704 Mar-26-2019, 11:37 AM
Last Post: perfringo
  Help with finding vowels nlord7 1 2,271 Feb-27-2019, 04:40 AM
Last Post: ichabod801
  Print The Length Of The Longest Run in a User Input List Ashman111 3 3,213 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk
  Help Formatting Print Statement (input from 3 lists) X 2 Hebruiser 11 6,349 Dec-06-2017, 04:47 PM
Last Post: gruntfutuk
  Functions to remove vowels and extra blanks RiceGum 10 5,878 Nov-17-2017, 05:40 PM
Last Post: heiner55
  How to add user input together then print the result Liwuid_Ocelot 6 5,437 Mar-22-2017, 02:18 AM
Last Post: Liwuid_Ocelot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020