Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Alternate vowels and consonants in Python Strings?
Alternating vowels and consonants in a string means rearranging characters so that consonants and vowels appear in alternating positions. Python provides several approaches using built-in functions like zip(), lambda functions, and zip_longest().
Using join() and zip() Methods
This approach separates vowels and consonants into different lists, then combines them alternately using zip() ?
Algorithm
Step 1 ? Initialize two empty lists for vowels and consonants
Step 2 ? Iterate through the string to separate vowels and consonants
Step 3 ? Use
zip()to pair consonants with vowelsStep 4 ? Use
join()to create the alternating string
Example
# Empty lists to hold vowels and consonants
vow_str = []
con_str = []
# Input string
str_1 = "Welcome to Paris"
# Iterate through the string
for char in str_1:
# Check if character is alphabetic
if char.isalpha():
if char.lower() in 'aeiou':
# Add vowels to vowel list
vow_str.append(char)
else:
# Add consonants to consonant list
con_str.append(char)
# Alternate consonants and vowels using zip()
output_str = ''.join([a + b for a, b in zip(con_str, vow_str)])
print("Original string:", str_1)
print("Alternating result:", output_str)
Original string: Welcome to Paris Alternating result: WelocemotaPi
Using Lambda Functions
Lambda functions provide a concise way to filter vowels and consonants from the string ?
Example
# Input string
str_1 = "Welcome Home"
# Define vowels
vowels = 'aeiou'
# Lambda functions to filter vowels and consonants
vowel_filter = lambda char: char.lower() in vowels and char.isalpha()
consonant_filter = lambda char: char.lower() not in vowels and char.isalpha()
# Filter vowels and consonants
vow_str = list(filter(vowel_filter, str_1))
con_str = list(filter(consonant_filter, str_1))
# Create alternating string
output_str = ''.join([a + b for a, b in zip(con_str, vow_str)])
print("Original string:", str_1)
print("Vowels found:", vow_str)
print("Consonants found:", con_str)
print("Alternating result:", output_str)
Original string: Welcome Home Vowels found: ['e', 'o', 'e', 'o', 'e'] Consonants found: ['W', 'l', 'c', 'm', 'H', 'm'] Alternating result: WelocemoHme
Using zip_longest() Function
The zip_longest() function handles cases where vowels and consonants have different counts by filling missing values ?
Example
from itertools import zip_longest
# Input string
str_1 = "Welcome home"
# Define vowels
vowels = 'aeiou'
# Separate vowels and consonants using list comprehension
vow_str = [char for char in str_1 if char.lower() in vowels]
con_str = [char for char in str_1 if char.isalpha() and char.lower() not in vowels]
# Use zip_longest to handle unequal lengths
output_str = ''.join([a + b for a, b in zip_longest(con_str, vow_str, fillvalue='')])
print("Original string:", str_1)
print("Vowels:", vow_str)
print("Consonants:", con_str)
print("Alternating result:", output_str)
Original string: Welcome home Vowels: ['e', 'o', 'e', 'o', 'e'] Consonants: ['W', 'l', 'c', 'm', 'h', 'm'] Alternating result: WelocemohmE
Comparison
| Method | Handles Unequal Lengths | Best For |
|---|---|---|
zip() |
No (truncates) | Equal vowel/consonant count |
Lambda + filter()
|
No (truncates) | Functional programming style |
zip_longest() |
Yes (fills missing) | Unequal vowel/consonant count |
Conclusion
Use zip_longest() when vowels and consonants have different counts. For equal counts, zip() is simpler. Lambda functions provide a functional approach to filtering characters.
