Open In App

Python - Split String on vowels

Last Updated : 02 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a String, perform split on vowels. 

Example:

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

Input : test_str = 'GFGaBstuforigeeks' 
Output : ['GFG', 'Bst', 'f', 'r', 'g', 'ks']
Explanation : a, e, o, u, i are vowels and split happens on that.

Naive approach: 

  • Initialize variable vowels to contain all the vowels.
  • Initialize an empty list result and a variable temp to an empty string.
  • Iterate through each character in the input string test_str.
  • For each character, check if it is a vowel (by checking if it is in the vowels variable).
  • If the character is a vowel and the temp variable is not empty, append temp to the result list and reset temp to an empty string.
  • If the character is not a vowel, add it to the temp variable.
  • After the iteration, if the temp variable is not empty, append it to the result list.
  • Return the result list.
Python3
def split_on_vowels(test_str):
    vowels = 'aeiouAEIOU'
    result = []
    temp = ""
    for char in test_str:
        if char in vowels:
            if temp != "":
                result.append(temp)
                temp = ""
        else:
            temp += char
    if temp != "":
        result.append(temp)
    return result

test_str = 'GFGaBstuforigeeks'
print(split_on_vowels(test_str))

Output
['GFG', 'Bst', 'f', 'r', 'g', 'ks']

Time Complexity: O(N)
Auxiliary Space: O(N)

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
# Python3 code to demonstrate working of 
# Split String on vowels
# Using split() + regex
import re

# initializing strings
test_str = 'GFGaBste4oCS'

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

# splitting on vowels 
# constructing vowels list
# and separating using | operator
res = re.split('a|e|i|o|u', test_str)

# printing result 
print("The splitted string : " + str(res)) 

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

Time Complexity: O(n), where n is the length of the string "test_str". The "re.split" function splits the string by searching for specified characters (vowels), which takes linear time proportional to the length of the string. 
Auxiliary space: O(1), as it uses a constant amount of memory regardless of the size of the input string "test_str".

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

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

Python3
# Python3 code to demonstrate working of
# Split String on vowels

# initializing strings
test_str = 'GFGaBste4oCS'

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

# splitting on vowels
vow="aeiouAEIOU"
for i in test_str:
    if i in vow:
        test_str=test_str.replace(i,"*")
res=test_str.split("*")

# printing result
print("The splitted string : " + str(res))

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

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using replace(),split() and ord() methods

Python3
# Python3 code to demonstrate working of
# Split String on vowels

# initializing strings
test_str = 'GFGaBste4oCS'

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

# splitting on vowels
x=[97, 101, 105, 111, 117, 65, 69, 73, 79, 85]
for i in test_str:
    if ord(i) in x:
        test_str=test_str.replace(i,"*")
res=test_str.split('*')
# printing result
print("The splitted string : " + str(res))

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

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 : Using operator.countOf() method

Python3
import operator as op


def split_on_vowels(test_str):
    vowels = 'aeiouAEIOU'
    result = []
    temp = ""
    for char in test_str:
        if op.countOf(vowels, char) > 0:
            if temp != "":
                result.append(temp)
                temp = ""
        else:
            temp += char
    if temp != "":
        result.append(temp)
    return result


test_str = 'GFGaBstuforigeeks'
print(split_on_vowels(test_str))

Output
['GFG', 'Bst', 'f', 'r', 'g', 'ks']

Time Complexity: O(n)
Auxiliary Space: O(n)

 Method5# :using the 'itertools.groupby 'function from the 'itertools' module

Python3
import itertools

# Define the string to be split
test_str = 'GFGaBste4oCS'

# Define the vowels to split the string on
vowels = 'aeiouAEIOU'

# Print the original string
print("The original string is:", test_str)

# Use itertools.groupby to group adjacent characters in test_str based on if they are in vowels
res = [list(g) for k, g in itertools.groupby(test_str, key=lambda x: x not in vowels) if k]

# Join each group of characters into a string
res = [''.join(substring) for substring in res]

# Print the final split string
print("The split string is:", res)
#this code is contributed by Asif_shaik

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

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6: Using translate method

Algorithm:

  1. Initialize a string test_str.
  2. Create a translation table that replaces vowels with spaces using the str.maketrans() method.
  3. Apply the translation table to the test_str using the translate() method and store it in a variable named trans_str.
  4. Split the trans_str on spaces and store the result in a list named res.
  5. Print the result.
Python3
# initializing string
test_str = 'GFGaBste4oCS'

# create a translation table that replaces vowels with spaces
trans_table = str.maketrans('aeiouAEIOU', ' '*10)

# split the string on spaces
res = test_str.translate(trans_table).split()

# printing result
print("The splitted string : " + str(res))
#This code is contributed by Vinay Pinjala.

Output
The splitted string : ['GFG', 'Bst', '4', 'CS']

Time complexity: The time complexity of this code is O(n) because the maketrans(), translate() and split() methods take linear time.
Auxiliary Space: The space complexity of this code is O(n) because we are creating a new string and a new list to store the results.


Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy