Deleting consonants from a string in Python

前端 未结 3 778
情深已故
情深已故 2020-12-01 20:01

Here is my code. I\'m not exactly sure if I need a counter for this to work. The answer should be \'iiii\'.

def eliminate_consonants(x):
                


        
3条回答
  •  自闭症患者
    2020-12-01 20:57

    == tests for equality. You are looking to see if any of the characters exist in the string that are in your list 'vowels'. To do that, you can simply use in such as below.

    Additionally, I see you have a 'vowels_found' variable but are not utilizing it. Below one example how you can solve this:

    def eliminate_consonants(x):
        vowels= ['a','e','i','o','u']
        vowels_found = 0
        for char in x:
            if char in vowels:
                print(char)
                vowels_found += 1
    
        print "There are", vowels_found, "vowels in", x
    
    eliminate_consonants('mississippi')
    

    Your output would then be:

    i
    i
    i
    i
    There are 4 vowels in mississippi
    

提交回复
热议问题