
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alternate Vowels and Consonants in Python Strings
The words are composed of vowels and consonants that form the strings. Using the Python language, the vowels and consonants can be varied inside the string. The methods to do this can be varying and some may be simple loop iteration on the other side it can be some of the functions and methods. In this article, the user will learn how to alternate vowels and consonants in Python Strings. Three approaches are depicted in this article. Multiple inbuilt functions are available in the python language to resolve the complex problem.
Let's take a deep dive into this article.
Approach
Approach 1: Using join() and zip() method
Approach 2: Using lambda function
Approach 3: Using the zip_longest() method
Approach 1: Python Program to Alternate vowels and consonants using join() and zip() method
The functions used to perform the alternating vowels and consonants of the string are join() and zip() method. The list comprehension iterates through the strings of the list.
Algorithm
Step 1 ? Two empty lists are initialized, one for vowels and the other for consonants.
Step 2 ? for loop is used to filter the vowels and consonants from the string
Step 3 ? Then depending on the vowels and consonants present in the string, then it is altered using the slicing method.
Step 4 ? And the new string uses the join() method to append the vowels and consonants together.
Step 5 ? The printing statement will return the altered string.
Example
#Empty list is initialized to hold the vowels and consonants vow_str = [] con_str = [] #the user is prompted to enter the string str_1 = "Welcome to Paris" #for loop is used to iterate through the string for char in str_1: #checking whether the character of the string is lower if char.isalpha(): if char.lower() in 'aeiou': #append() function is used to add the vowels of the string vow_str.append(char) else: #append() function is used to add the consonants of the string con_str.append(char) #join() method is assigned to the output_str to add both the vowels and consonants output_str = ''.join([a+b for a,b in zip(con_str,vow_str)]) #print statement returns the strings after altering vowels and consonants print(output_str)
Output
WelocemotaPi
Approach 2: Python Program to Alternate vowels and consonants using lambda function
The functions used to perform the alternating vowels and consonants of the string are join() and zip() method along with the key parameter of the lambda function. The list comprehension iterates through the strings of the list.
Algorithm
Step 1 ? Initialized the variable named vowels.
Step 2 ? Then depending on the vowels and consonants present in the string, then it is altered using the lambda function.
Step 3 ? The vowels and consonants are filtered with the help of the lambda function.
Step 4 ? And the new string uses the join() method to append the vowels and consonants together.
Step 5 ? The printing statement will return the altered string.
Example
#the variable vowel is initialized vowels = 'aeiou' #lambda function filters only the vowels from the string vowel_filter = lambda char: char.lower() in vowels #lambda function filters only the consonants from the string consonant_filter = lambda char: char.lower() not in vowels #the filtered vowels is listed vow_str = list(filter(vowel_filter, str_1)) #the filtered consonants is listed con_str = list(filter(consonant_filter, str_1)) #join() method is assigned to the output_str to add both the vowels and consonants output_str = ''.join([a+b for a,b in zip(con_str,vow_str)]) #print statement returns the strings after altering vowels and consonants print(output_str)
Output
Enter the string: Welcome Home WelocemoHe
Approach 3: Python Program to Alternate vowels and consonants using the zip_longest() function
The for loop is used to find the vowels and consonants and later store them in the assigned variables.
Algorithm
Step 1 ? The itertools module is imported to use the zip_longest() function.
Step 2 ? for loop is used to filter the vowels and consonants from the string
Step 3 ? Then depending on the vowels and consonants present in the string, it is filtered.
Step 4 ? And the new string uses the join() method to append the vowels and consonants together.
Step 5 ? The printing statement will return the altered string.
Example
#importing the itertools function to use the zip_longest function from itertools import zip_longest #the user is prompted to enter the string str_1 = "Welcome home " #the variable vowel is initialized vowels = 'aeiou' #vowel and consonants are assigned to get it from the string vow_str = [char for char in str_1 if char.lower() in vowels] con_str = [char for char in str_1 if char.lower() not in vowels] #join() method is assigned to the output_str to add both the vowels and consonants output_str = ''.join([a+b for a,b in zip_longest(con_str,vow_str, fillvalue='')]) #print statement returns the strings after altering vowels and consonants print(output_str)
Output
Welocemo ehm
Conclusion
Python Language comes under the OOPS concept and it runs the code immediately without checking for errors. Apart from the other languages it has got its unique place because of its advantages like it's easy to code with simple syntax and statements.