Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Vowel indices in String
Next article icon

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.


Next Article
Python | Vowel indices in String
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python | Split Sublist Strings
    Yet another variation of splitting strings is splitting the strings that are an element of the sublist. This is quite a peculiar problem, but one can get the data in this format, and the knowledge of splitting it anyways is quite useful. Let's discuss certain ways in which this particular task can b
    5 min read
  • Python | Vowel indices in String
    Sometimes, while working with Python Strings, we can have a problem in which we need to extract indices of vowels in it. This kind of application is common in day-day programming. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop This is one way in which this task
    6 min read
  • Split and join a string in Python
    The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together
    3 min read
  • Python | Exceptional Split in String
    Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should
    4 min read
  • Python - Selectively Split in Strings
    Sometimes, while working with Python strings, we may have to perform a split. Not sometimes, normal one, depending on deliminator but something depending upon programming constructs like elements, numbers, words etc and segregate them. Lets discuss a way in which this task can be solved. Method : Us
    3 min read
  • Python - Reversed Split Strings
    In Python, there are times where we need to split a given string into individual words and reverse the order of these words while preserving the order of characters within each word. For example, given the input string "learn python with gfg", the desired output would be "gfg with python learn". Let
    3 min read
  • Python | Split flatten String List
    Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
    7 min read
  • Recursively Count Vowels From a String in Python
    Python is a versatile and powerful programming language that provides various methods to manipulate strings. Counting the number of vowels in a string is a common task in text processing. In this article, we will explore how to count vowels from a string in Python using a recursive method. Recursion
    3 min read
  • Python | K Character Split String
    The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Let’s discuss certain
    4 min read
  • Python | Ways to split a string in different ways
    The most common problem we have encountered in Python is splitting a string by a delimiter, But in some cases we have to split in different ways to get the answer. In this article, we will get substrings obtained by splitting string in different ways. Examples: Input : Paras_Jain_Moengage_best Outpu
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences