Reputation: 152
I need to return all non-vowel characters first, then vowels last from any given string. This is what I have so far, which is printing non-vowels first, but not printing any vowels after:
# Python 3
# Split a string: consonants/any-char (1st), then vowels (2nd)
def split_string():
userInput = input("Type a word here: ")
vowels = "aeiou"
for i in userInput:
if i not in vowels:
print(i, end="")
i += i
# else:
# if i in vowels:
# print(i, end="")
# i = i+i
# This part does not work, so I commented it out for now!
return(userInput)
input = split_string()
Answered! I simply needed a second loop that is not nested inside the first loop.
def split_string():
userInput = input("Type a word here: ")
vowels = "aeiou"
for i in userInput:
if i not in vowels:
print(i, end="")
i += i
for i in userInput:
if i in vowels:
print(i, end="")
return(userInput)
input = split_string()
Upvotes: 0
Views: 928
Reputation: 27231
Here's an idiomatic answer.
def group_vowels(word):
vowels = [x for x in word if x in "aeiou"]
non_vowels = [x for x in word if x not in "aeiou"]
return vowels, non_vowels
word = input("Type a word here: ")
vowels, non_vowels = group_vowels(word)
print("".join(non_vowels))
print("".join(vowels))
Notice:
group_vowels
returns a list of vowels and a list of non-vowels.join
to concatenate together a list of characters into a single string.Upvotes: 1