Skip to content
Related Articles

Related Articles

Python – Split String on vowels

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Jul, 2022

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




# 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']

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']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!