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 Reverse String Consonants without Affecting other Elements in Python
String consonants refer to the non-vowel sounds produced when pronouncing a string of characters. In this article, we will learn how to reverse only the consonants in a string without affecting the position of vowels and other characters using two different methods in Python.
Problem Example
Given a string, we want to reverse only the consonants while keeping vowels and special characters in their original positions ?
# Input and expected output
input_string = "tutorialspoint"
print("Before Reversing:", input_string)
# After reversing consonants: "tunopiaslroitt"
Before Reversing: tutorialspoint
Method 1: Using append() and join() Functions
This approach first collects all consonants, then replaces them in reverse order ?
def checkConsonant(char):
# List of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
# Check if character is not a vowel and is alphabetic
return char.lower() not in vowels and char.isalpha()
def consonantReversing(inputString):
# Convert string to list of characters
char_list = list(inputString)
# Collect all consonants
consonants = []
for char in char_list:
if checkConsonant(char):
consonants.append(char)
# Replace consonants in reverse order
consonant_index = len(consonants) - 1
for i in range(len(char_list)):
if checkConsonant(char_list[i]):
char_list[i] = consonants[consonant_index]
consonant_index -= 1
return "".join(char_list)
# Test the function
print(consonantReversing("tutorialspoint"))
print(consonantReversing("python codes"))
tunopiaslroitt sdc onhtoyep
Method 2: Using pop() Function and String Concatenation
This method uses pop() to remove consonants from the end of a list while building the result string ?
def consonantReversing(inputString):
# Collect all consonants using list comprehension
consonants = [c for c in inputString if c.lower() in 'bcdfghjklmnpqrstvwxyz']
result = ""
# Build result string
for char in inputString:
if char.lower() in 'bcdfghjklmnpqrstvwxyz':
# Use pop() to get consonant from the end
result += consonants.pop()
else:
# Keep vowels and special characters as is
result += char
return result
# Test the function
print(consonantReversing("tutorialspoint"))
print(consonantReversing("python codes"))
print(consonantReversing("Hello World!"))
tunopiaslroitt sdcnoh toyep dlroW olleH!
Comparison
| Method | Space Complexity | Best For |
|---|---|---|
| append() + join() | O(n) | Clear separation of logic |
| pop() + concatenation | O(n) | Single-pass solution |
How It Works
Both methods follow the same core principle:
- Identify consonants: Extract all consonant characters from the string
- Reverse order: Use the consonants in reverse order during replacement
- Preserve positions: Keep vowels and special characters in their original positions
Conclusion
Both methods effectively reverse consonants while preserving vowel positions. The append() method offers clearer logic separation, while pop() provides a more concise single-pass solution. Choose based on your preference for readability versus brevity.
