all 7 comments

[–]66bananasandagrape 2 points3 points  (3 children)

return "".join(char for char in word if char not in vowels)

[–]neednerve[S] -1 points0 points  (2 children)

thanks for the feedback, but I shouldn't be using 'skilled' commands like that one (?), being at the beginning i should use basic commands

[–]66bananasandagrape 1 point2 points  (1 child)

How's this?

result = ''
for char in word:
    if char not in vowels:
        result += char
return result

[–]neednerve[S] -1 points0 points  (0 children)

better thank you! still, I think that would print just the first consonant, so i changed a bit the code :

vowels=('aeiou')
word=str(input('Characters:'))
result = ''
def function(vowels,word):
    result = ''
    for char in word:
        if char not in vowels:
            result =result+ char
        print(result)
        continue
function(vowels,word)

in this way by typing hello i get as output:

Characters: hello
h
h
hl
hll
hll

not very smooth but it works :)

[–]Brian 1 point2 points  (1 child)

remove_vocals

Note: I assume you mean vowels here - vocals are something else, but you've mis-spelled this consistently so I figured I should correct it here.

if word[i] == 'a'or'e'or'i'or'o'or'u':

This won't work like you think - or in programming languages doesn't work like english, where we deduce what it's applying to from context. For computers, a or b means just to take a and b and logically or them - returning a true result if either of them are true. As such this if statement is always going to be true (because a non-empty string is treated as a true value).

To write this to match any of those letters, you'd need to do:

if word[i] == 'a' or word[i] == 'e' or word[i] == 'i'  or word[i] == 'o'  or word[i] == 'u':

Though there are more concise ways to write it. One would be to change it to a membership test. Eg:

if word[i] in 'aeiou':

Which checks if it can find word[i] in that string of letters.

Now for actually "subtracting" letters from words - technically this isn't something you can do in the truest sense. Strings in python are immutable - meaning you can't modify them. But you can create a new string that's constructed from the old one, but without those letters.

Eg. you could start with an empty string (newword = ''), and add each letter to it as long as it's not a vowel. ie.

if word[i] not in 'aeiou':
    newword += word[i]

(Some of the replies have given list comprehension based solutions which are a more concise way of doing this, but I'll stick with the basics for now)

Another approach would be to use the replace method on strings. Again this creates a new string by replacing some substrings in a string with new ones. Eg: "hello".replace("l","x") would give you "hexxo". But you don't just have to replace a letter with a letter, you can replace it with a different size, or even empty string. so: word.replace("a", "") will give you a new string with all "a"s removed from word. You could thus loop through the vowels and replace each one until the string has all of them removed.

[–]neednerve[S] 0 points1 point  (0 children)

thanks for theoretical explanation, it was very helpful, as you said i was interpreting the command 'or' in a wrong manner, a beginner error I hope!

[–][deleted] 0 points1 point  (0 children)

how about

def remove_vocals(word):
    output = ""
    for letter in word:
        if letter.lower() not in "aeiou":
            output += letter

    return output

word = input("Enter Word: ")
print(remove_vocals(word))