2

Good evening everyone..... i wrote a vowel eater program with the code below

wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
for letter in userWord:
    if letter == 'A':
        continue
    elif letter == 'E':
        continue
    elif letter == 'I':
        continue
    elif letter == 'O':
        continue
    elif letter == 'U':
        continue
    else:
        print(letter)

It run fine but i want to use concatenation operation to ask python to combine selected letters into a longer string during subsequent loop turns, and assign it to the wordWithoutVowels variable..... I really appreciate any help or suggestions thanks in advance

Abdulrahman Isah
  • 19
  • 1
  • 2
  • 4
  • 2
    Does this answer your question? [Remove specific characters from a string in Python](https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python) – Janez Kuhar May 20 '20 at 22:26
  • `eater = lambda s: ''.join(filter(lambda c: c.upper() not in {'A', 'E', 'I', 'O', 'U'}, s))` – erip May 20 '20 at 22:27
  • Thanks but I just need a simple concatenation in the loop @ Janez Kuhar – Abdulrahman Isah May 20 '20 at 22:32

10 Answers10

3

is this what you need?

wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()
for letter in userWord:
    if letter == 'A':
        word = letter
        continue
    elif letter == 'E':
        continue
    elif letter == 'I':
        continue
    elif letter == 'O':
        continue
    elif letter == 'U':
        continue
    else:
        wordWithoutVowels+=letter

print(wordWithoutVowels)
jv95
  • 681
  • 4
  • 18
2

Another approach. You can prepare a set of vowels you want to filter-out before-hand and then use str.join() to obtain you string:

userWord = input("Please Enter a word: ")
vowels = set('aeiou')

wordWithoutVowels = ''.join(character for character in userWord if not character.lower() in vowels)

print(wordWithoutVowels)

Prints (for example):

Please Enter a word: Hello World
Hll Wrld
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

I'm sorry I didn't read the original post (OP) more carefully. Poster clearly asked for a way to do this by concatenation in a loop. So instead of excluding vowels, we want to include the good characters. Or instead of looking for membership, we can look for not in membership.

wordWithoutVowels = ""
userWord = input("Please Enter a word: ")

vowels = 'aAeEiIoOuU'

wordWithoutVowels = '' # initialize to empty string

for letter in userWord:
    if letter not in vowels:
        wordWithoutVowels += letter  # equivalent to wordWithoutVowels = wordWithoutVowels + letter

print(wordWithoutVowels)

Please Enter a word: Hello World
Hll Wrld
bfris
  • 5,272
  • 1
  • 20
  • 37
1

or you can try this:

wordWithoutVowels = ""

user = input("Enter a word: ")
userWord  = user.upper()


for letter in userWord:
    if letter == "A":
        continue
    elif letter == "E":
        continue
    elif letter == "O":
        continue
    elif letter == "I":
        continue
    elif letter == "U":
        continue
    else:
        wordWithoutVowels += letter

print(wordWithoutVowels)
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
dco james
  • 11
  • 1
0

Using str.replace() seems like a natural way to go for a problem like this

Brute Force
Just go through all of the vowels. And if they exist in input string, replace them

wordWithoutVowels = ""
userWord = input("Please Enter a word: ")
userWord = userWord.upper()

# make a copy of userWord
output = userWord

# replace vowels
output = output.replace('A', '') # replace A with empty string
output = output.replace('E', '') # replace E with empty string
output = output.replace('I', '') # replace I with empty string
output = output.replace('O', '') # replace O with empty string
output = output.replace('U', '') # replace U with empty string

print(output)

Please Enter a word: Hello World
HLL WRLD

Use a loop
This is a little more elegant. And you won't have to convert the input to uppercase.

wordWithoutVowels = ""
userWord = input("Please Enter a word: ")

# make a copy of userWord
output = userWord

# replace vowels
vowels = 'aAeEiIoOuU'
for letter in vowels:
    output = output.replace(letter, '') # replace letter with empty string

print(output)

Please Enter a word: Hello World
Hll Wrld
bfris
  • 5,272
  • 1
  • 20
  • 37
0

Try this. I think it's the easiest way:

word_without_vowels = ""
user_word = input("Enter a word")
user_word = user_word.upper()

for letter in user_word:
    # Complete the body of the loop.
    if letter in ("A","E","I","O","U"):
        continue
    word_without_vowels+=letter
print(word_without_vowels)
0
user_word =str(input("Enter a Word"))
# and assign it to the user_word variable.
user_word = user_word.upper()
vowels = ('A','E','E','I','O','U')
for letter in user_word:
    if letter in vowels:
        continue
    elif letter == vowels:
        letter = letter - vowels
    else:
        print(letter)
0

A one liner that does what you need is:

wordWithoutVowels = ''.join([ x for x in userWord if x not in 'aeiou'])

or the equivalent:

wordWithoutVowels = ''.join(filter(lambda x : x not in 'aeiou', userWord))

The code is creating a list containing the letters in the string that are not vowels and then joining them into a new string.

You just need to figure out how to handle the lower/capital cases. You could do x.lower() not in 'aeiou', check if x is in 'aeiouAEIOU', ... you have plenty of choices.

Gerardo Zinno
  • 1,518
  • 1
  • 13
  • 35
0
user_word = input('enter a word:')
user_word = user_word.upper()

for letter in user_word:
    if letter in ('A','E','I','O','U'):
        continue
    print(letter)
Sebo
  • 1
  • 2
0
word_without_vowels = ""
vowels = 'A', 'E', 'I', 'O', 'U'

user_word = input('enter a word:')
user_word = user_word.upper()

for letter in user_word:
    if letter in vowels:
        continue
    word_without_vowels += letter
print(word_without_vowels)
Sebo
  • 1
  • 2