+ 1

string filtering

Hi, The code supposes to filter vowels and consonants from a given word. filtered vowels and consonants suppose to be printed as: word_vowels and word_consonats code looks as follow: What is wrong? word = 'pasjonat' def alphabet_filter(word): vowels = ('a', 'e', 'i', 'o', 'u') word_vowels = '' for x in word: if x in vowels: word_consonants = word.replace(x, '') word = word_consonants for y in word: if y not in vowels: vowel = str(y) word_vowels += vowel print(word_consonants) print(word_vowels) alphabet_filter(word)

5th Jan 2020, 7:42 PM
Rafał Milczarski
Rafał Milczarski - avatar
2 odpowiedzi
+ 5
I would recommend you to rework the code, so that it gets more readable: # version 1: vow = 'aeiou' word = 'pasjonat' vowels = [] consonants = [] for char in word: if char in vow: vowels.append(char) else: consonants.append(char) print(''.join(vowels)) print(''.join(consonants)) # or version 2 as a comprehension: vowels = [x for x in word if x in vow] consonants = [x for x in word if x not in vow] print(''.join(vowels)) print(''.join(consonants))
5th Jan 2020, 9:22 PM
Lothar
Lothar - avatar
+ 1
thank you Lothar! your code makes the case much easier to understand !
10th Jan 2020, 3:28 PM
Rafał Milczarski
Rafał Milczarski - avatar