Python – 在元音上分割字符串
给定一个字符串,对元音执行拆分。
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.
方法:使用 regex() + split()
在此,我们使用正则表达式 split(),它接受多个字符来执行拆分,传递元音列表,对字符串执行拆分操作。
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))
输出
The original string is : GFGaBste4oCS
The splitted string : ['GFG', 'Bst', '4', 'CS']