How to make join consonant and vowel if vowel is come after consonant in python

vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’] consonants = [‘b’, ‘c’, ‘d’, ‘f’, ‘g’, ‘h’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]

I want this Output

suppose if Input is “This is our Home”

[‘T’,‘hi’,‘s’,‘i’,‘s’,‘o’,‘u’,‘r’,‘ho’,‘me’]

“something is better than Everything” [‘so’,‘me’,‘t’,hi’,‘n’,‘g’,‘i’,‘s’,‘be’,‘t’,‘te’,‘r’,‘t’,‘ha’,‘n’,‘E’,‘ve’,‘r’,‘y’,‘t’,‘hi’,‘n’,‘g’]

def group_vowels(string): output = substring = ‘’

for i, c in enumerate(string, start=1):
    print('ic',i ,c)
    
    if c in consonants:
        a = substring
        substring = substring + c
        
        output.append(substring)
        substring = ''
      

   
    if c in vowels:
        substring = substring+c
    
        if i == len(string): output.append(substring)
return output

print (group_vowels(“This is our Home”))

Let us try some easy examples:

>>> from cons1 import group_vowels as gv
>>> gv("ll")
ic 1 l
ic 2 l
['l', 'l']
>>> gv("la")
ic 1 l
ic 2 a
['l', 'a']
>>> gv("al")
ic 1 a
ic 2 l
['al']

So it puts a consonant after a vowel, but not a vowel after your consonant. So the appending code is in the wrong branch.

To fix that a moved the cleaning of the substring to the other branch:

        if c in consonants:
            a = substring
            substring = substring + c

            output.append(substring)

        if c in vowels:
            substring = substring+c

            if i == len(string): output.append(substring)
            substring = ''

This gives us:

>>> gv("ll")
ic 1 l
ic 2 l
['l', 'll']
>>> gv("la")
ic 1 l
ic 2 a
['l', 'la']
>>> gv("al")
ic 1 a
ic 2 l
['l']

It now gives you indeed the combination of consonants and vowels, but always outputs consonants that are followed by a vowel twice. Not good. So if we read a consonant we need to wait until the next character to know what to print. The “lonely last character” appending also goes to the other branch.

So we get:

        if c in consonants:
            a = substring
            #last read was a consonant
            print(i, substring)
            if substring :
                output.append(substring)
            substring = c
            if i == len(string):
                output.append(substring)

        if c in vowels:
            substring = substring+c

            output.append(substring)
            substring = ''

We also always output on a vowel. If it is a “single” vowel, we append, it otherwise, there is a consonant in substring and we want to append the pair.

What do you want to do with the a variable? It serves no purpose now.

The test of your sentence:

>>> gv("This is our Home")
2 
4 
7 
11 
15 
['hi', 'si', 'so', 'u', 'ro', 'me']

I leave dealing with spaces and capital letters to you :wink:

1 Like

vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’] consonants = [‘b’, ‘c’, ‘d’, ‘f’, ‘g’, ‘h’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]

I want this Output

suppose if Input is “This is our Home”

[‘T’,‘hi’,‘s’,‘i’,‘s’,‘o’,‘u’,‘r’,‘ho’,‘me’]

“something is better than Everything” [‘so’,‘me’,‘t’,hi’,‘n’,‘g’,‘i’,‘s’,‘be’,‘t’,‘te’,‘r’,‘t’,‘ha’,‘n’,‘E’,‘ve’,‘r’,‘y’,‘t’,‘hi’,‘n’,‘g’]

def group_vowels(string): output = substring = ‘’

for i, c in enumerate(string, start=1):
    print('ic',i ,c)
    
    if c in consonants:
        a = substring
        substring = substring + c
        
        output.append(substring)
        substring = ''
      

   
    if c in vowels:
        substring = substring+c
    
        if i == len(string): output.append(substring)
return output

print (group_vowels(“This is our Home”))

Its Work for me thank you so much ,I already fixed myself this problem But its too shortcut and easy I used this Thanks a lot …

I want to know is there any possibility to assign joined two character to one character Which mean if output is
from
“This is our Home”

[‘hi’, ‘si’, ‘so’, ‘u’, ‘ro’, ‘me’]

can I set ‘hi’ is one Character and told to python if user type any string this two letter is one character

Python does not have a “character” type. What you are asking here is unclear to me. There are just strings in Python, and these range in length from 0 ("") to infinity and a length one string is in only one way a special case: When you iterate over a string, the string is split in strings of length 1.