Sometimes, while working with Strings in Python, we can have a problem in which we may need restructure a string, adding alternate vowels and consonants in it. This is a popular school-level problem and having solution to this can be useful. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + join() + zip_longest()
The combination of above functions can be used to perform this task. In this, we first, separate vowels and consonents in separate lists. And then join alternatively using zip_longest() and join().
# Python3 code to demonstrate working of # Alternate vowels and consonents in String # using zip_longest() + join() + loop from itertools import zip_longest # initializing string test_str = "gaeifgsbou" # printing original string print("The original string is : " + test_str) # Alternate vowels and consonents in String # using zip_longest() + join() + loop vowels = ['a', 'e', 'i', 'o', 'u'] test_vow = [] test_con = [] for ele in test_str: if ele in vowels: test_vow.append(ele) elif ele not in vowels: test_con.append(ele) res = ''.join(''.join(ele) for ele in zip_longest(test_vow, test_con, fillvalue ='')) # printing result print("Alternate consonents vowels are: " + res) |
The original string is : gaeifgsbou Alternate consonents vowels are: agefigosub
Method #2 : Using loop + map() + lambda
This task can also be performed using combination of above functionalities. It is similar as above method, the only differences are usage of map() and lambda functions to perform alternate joining.
# Python3 code to demonstrate working of # Alternate vowels and consonents in String # using loop + map() + lambda from itertools import zip_longest # initializing string test_str = "gaeifgsbou" # printing original string print("The original string is : " + test_str) # Alternate vowels and consonents in String # using loop + map() + lambda vowels = ['a', 'e', 'i', 'o', 'u'] test_vow = [] test_con = [] for ele in test_str: if ele in vowels: test_vow.append(ele) elif ele not in vowels: test_con.append(ele) res = ''.join(map(lambda sub: sub[0] + sub[1], zip_longest(test_vow, test_con, fillvalue =''))) # printing result print("Alternate consonents vowels are: " + res) |
The original string is : gaeifgsbou Alternate consonents vowels are: agefigosub
Recommended Posts:
- Python | Count and display vowels in a string
- Python | Alternate cases in String
- Python | Alternate Sort in String list
- Python program to count number of vowels using sets in given string
- Python - Convert Alternate String Character to Integer
- Modify the string such that it contains all vowels at least once
- Python - Alternate Default Key Value
- Python | Program to accept the strings which contains all vowels
- Python | Alternate Cycling in list
- Python | Alternate character addition
- Python - Alternate elements Similarity
- Python - Alternate Strings Concatenation
- Python | Alternate Rear iteration
- Python | Alternate front - rear Sum
- Python | List Initialization with alternate 0s and 1s
- Python | Alternate element summation in list
- Python | Add element at alternate position in list
- Python | Maximize alternate element List
- Python | Multiplying Alternate elements in List
- Python IMDbPY - Getting alternate names of person
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

