
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
Reverse String Consonants Without Affecting Other Elements in Python
String consonants refer to the non-vowel sounds produced when pronouncing a string of characters or letters. In this article, we are going to study how to reverse only the consonants in a string without affecting the position of other elements using the following 2 methods in Python:
Using append() and join() functions
Using pop() function and string concatenation
Example
Before Reversing : tutorialspoint After Reversing: tunopiaslroitt
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
The function checkConsonant(char) is defined to check if a character is a consonant or not. It returns True if the character is not present in the list of vowels (['a', 'e', 'i', 'o', 'u']), otherwise it returns False.
The function consonantReversing(inputString) is defined to reverse the consonants in the input string.
The input string is converted into a list of characters using list(inputString).
An empty list consonantsList is created to store the consonants.
The list of characters is traversed using a for loop. For each character, the checkConsonant(char) function is called to check if it is a consonant. If it is a consonant, it is appended to the consonantsList.
The variable l is set to the length of consonantsList minus 1.
The substitution of consonants in the input string begins. For each character in the list, if it is a consonant (checked using checkConsonant()), it is replaced with the corresponding consonant from consonantsList using the index l. Then, l is decremented.
Finally, the modified list of characters is joined back into a string using "".join(new_list) and returned as the result.
The consonantReversing() function is called twice with different input strings to demonstrate the substitution of consonants.
Using append() and join() functions
The following code includes a function called consonantReversing that takes a string as input and reverses the consonants in the string while keeping the vowels unchanged. It accomplishes this by identifying the consonants in the input string, storing them in a list, and then substituting the consonants in the string with the consonants from the list in reverse order. The modified string is then returned as the output.
Example
# creating a function that checks if the string character # is consonant or not def checkConsonant(char): # list of vowels vowels = ['a', 'e', 'i', 'o', 'u'] # checking whether the character is not in vowels if char not in vowels: # returning true, if the condition is true return True # else returning false return False # creating another function that reverses the consonants of the string def consonantReversing(inputString): # converting the input string into list of characters new_list = list(inputString) # creating an empty list for storing consonants consonantsList = [] # traversing through the above list of characters for char in new_list: # checking if the current character is a consonant # by calling the checkConsonant function if checkConsonant(char): # appending that character to the consonants list # if the condition is true consonantsList.append(char) l = len(consonantsList)-1 # Substitute the consonants within a provided string by replacing them with elements # from a consonant list, starting from the last element in the list. for m in range(len(new_list)): if checkConsonant(new_list[m]): new_list[m] = consonantsList[l] l -= 1 return "".join(new_list) # calling the above consonantReversing() function by # passing the input string as an argument to it. print(consonantReversing("tutorialspoint")) # checking for the other string print(consonantReversing("python codes"))
Output
On executing, the above program will generate the following output
tunopiaslroitt sdc onhtoyep
Using pop() function and string concatenation
In the following example, the alternative approach identifies the consonants in the input string directly while iterating through the string, without using an additional function. It stores the consonants in a list and replaces them in reverse order during the second iteration. The rest of the characters, including non-consonants, are appended to the output string as is.
Example
def consonantReversing(inputString): consonants = [c for c in inputString if c.lower() in 'bcdfghjklmnpqrstvwxyz'] outputString = "" for char in inputString: if char.lower() in 'bcdfghjklmnpqrstvwxyz': outputString += consonants.pop() else: outputString += char return outputString # Example usage: print(consonantReversing("tutorialspoint")) print(consonantReversing("python codes"))
Output
tunopiaslroitt sdcnoh toyep
Conclusion
In conclusion, the provided Python program successfully reverses the consonants in a given string without affecting the position of other elements. It employs two different methods: one using append() and join() functions, and the other using pop() function and string concatenation. Both methods effectively identify and reverse the consonants while preserving the vowels and other characters in the string. These methods offer flexibility and can be applied to various scenarios where consonant reversal is required.