How do you separate vowels and consonants in a string in python?

How to Separate Vowels and Consonants in a String in Python | Previously, we had to check a character is a vowel or consonant. Here, we will discuss how to separate vowels and consonants in a string in Python.

In this program, we are using for loop and if-else statements to separate vowels and consonants in a string.

# Python program to separate vowels and consonants in a string

string = input('String: ')

print('Vowels: ')
for ch in string:
   if ch in "AEIOUaeiou":
      print(ch, end=', ')

print('\nConsonants: ')
for ch in string:
   if ch not in "AEIOUaeiou ":
      print(ch, end=', ')

Output for the input values test-case-1:-

String: Know Program
Vowels:
o, o, a,
Consonants:
K, n, w, P, r, g, r, m,

Output for the input values test-case-2:-

String: separate
Vowels:
e, a, a, e,
Consonants:
s, p, r, t,

How to Separate Vowels and Consonants in a String in Python

We use list comprehension methods to print the vowels and the consonants in the string.

# Python program to separate vowels and consonants in a string

def vowelsConsonants(string):
   # to count and print the vowels
   vowels = [each for each in string if each in "aeiouAEIOU"]
   print('Number of vowels:', len(vowels), vowels)
    
   # to count and print the consonants
   consonants = [each for each in string if each not in "aeiouAEIOU "]
   print('Number of consonants:', len(consonants), consonants)

# inputs and call function
string = input('String: ')
vowelsConsonants(string)

Output:-

String: Vowels Consonants
Number of vowels: 5 [‘o’, ‘e’, ‘o’, ‘o’, ‘a’]
Number of consonants: 11 [‘V’, ‘w’, ‘l’, ‘s’, ‘C’, ‘n’, ‘s’, ‘n’, ‘n’, ‘t’, ‘s’]

Well done for writing a question and having some code to post.

You didn't say what you expect find() to do, but I guess you expect it to return how many times it found something? Nope; count() does that; you could (word.count('a') + word.count('e')...) to solve this, but your hint is to use find(), so that's out.

find() returns where it found something, or -1 if it found nothing.

You're going to have to word.find('a') and then store the result, check if it's a location in the string or a -1 to say it found nothing. Then word.find('a', location+1) to search from just after the find location, and search the remaining characters. Check the return value of that to see if it found anything, then keep doing that in a loop until it finds nothing. Keep track of how many times it looped.

Then do that for 'e', 'i', 'o', 'u'. (a loop inside a loop).

Add them all up, that's the number of vowels. Take len(word) - num_vowels and that's the number of consonants...

unfinished example:

word = 'alfalfa'

location = word.find('a')
if location > -1: 
    print 'found "a", count this'

while location > -1:
    location = word.find('a', location + 1)
    if location > -1:
        print 'found another "a", count this'

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a String, perform split on vowels. 

    Input : test_str = ‘GFGaBst’ 
    Output : [‘GFG’, ‘Bst’] 
    Explanation : a is vowel and split happens on that.

    Input : test_str = ‘GFGaBstuforigeeks’ 
    Output : [‘GFG’, ‘Bst’, ‘for’, ‘geeks’] 
    Explanation : a, u, i are vowels and split happens on that.

    Method 1 : Using regex() + split()

    In this, we use regex split() which accepts multiple characters to perform split, passing list of vowels, performs split operation over string.

    Python3

    import re

    test_str = 'GFGaBste4oCS'

    print("The original string is : " + str(test_str))

    res = re.split('a|e|i|o|u', test_str)

    print("The splitted string : " + str(res))

    Output

    The original string is : GFGaBste4oCS
    The splitted string : ['GFG', 'Bst', '4', 'CS']

    Method 2 : Using replace() and split().

    First replace all vowels in string with “*” and then split the string by “*” as delimiter

    Python3

    test_str = 'GFGaBste4oCS'

    print("The original string is : " + str(test_str))

    vow="aeiouAEIOU"

    for i in test_str:

        if i in vow:

            test_str=test_str.replace(i,"*")

    res=test_str.split("*")

    print("The splitted string : " + str(res))

    Output

    The original string is : GFGaBste4oCS
    The splitted string : ['GFG', 'Bst', '4', 'CS']

    The Time and Space Complexity for all the methods are the same:

    Time Complexity: O(n)

    Auxiliary Space: O(n)


    How do you seperate a vowel from a string in Python?

    Method 1 : Using regex() + split() In this, we use regex split() which accepts multiple characters to perform split, passing list of vowels, performs split operation over string.

    How do you find vowels and consonants in Python?

    Method 1: Users can use built-in functions to check whether an alphabet is vowel function in python or not. Step 2: Using built-in python functions like (lower(), upper()), determine whether the input is vowel or consonant. Step 3: If the character is a vowel, it should be printed.

    How do you separate vowels from strings?

    The algorithm is as follows;.
    Algorithm. START Step-1: Input the string Step-3: Check vowel presence, if found return TRUE Step-4: Copy it to another array Step-5: Increment the counter Step-6: Print END. ... .
    Example. ... .
    Output..