Python program to reverse alternate characters in a string
Last Updated :
16 May, 2023
Given a String, reverse its alternate characters string.
Input : test_str = ‘geeks4rgeeks’
Output : keekr4sgeegs
Explanation : only g, e, s, r, e, k are reversed, rest all have same position.
Input : test_str = ‘geeks’
Output : egkes
Method #1 : Using loop + slicing + reversed()
This is one of the ways in which this task can be performed. In this, we extract alternates using slicing and then reverse the string using reversed. The reconstruction of the string is done using a loop.
Python3
test_str = 'geeks4rgeeks'
print ( "The original string is : " + str (test_str))
alt = test_str[:: 2 ]
not_alt = test_str[ 1 :: 2 ]
alt = "".join( reversed (alt))
res = ''
for idx in range ( len (alt)):
res + = alt[idx]
res + = not_alt[idx]
print ( "Is alternate reversed string : " + str (res))
|
Output
The original string is : geeks4rgeeks
Is alternate reversed string : keekr4sgeegs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension
This is one more way in which this task can be performed. In this, we perform a similar function as above by using one-liner functionality using list comprehension.
Python3
test_str = 'geeks4rgeeks'
print ( "The original string is : " + str (test_str))
res = " ".join([" ".join( reversed (test_str[:: 2 ]))[idx] + test_str[ 1 :: 2 ][idx]
for idx in range ( len ("".join( reversed (test_str[:: 2 ]))))])
print ( "Is alternate reversed string : " + str (res))
|
Output
The original string is : geeks4rgeeks
Is alternate reversed string : keekr4sgeegs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#3: Using join() and zip()
Python3
test_str = 'geeks4rgeeks'
print ( "The original string is : " + str (test_str))
alt = test_str[:: 2 ]
not_alt = test_str[ 1 :: 2 ]
alt = "".join( reversed (alt))
res = "".join([x + y for x, y in zip (alt, not_alt)])
print ( "Is alternate reversed string : " + str (res))
|
Output
The original string is : geeks4rgeeks
Is alternate reversed string : keekr4sgeegs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4: Using loop and string concatenation.
Python3
def alternate_reverse(string):
res = ''
for i in range ( len (string)):
if i % 2 = = 0 :
res = string[i + 1 ] + res
else :
res = string[i - 1 ] + res
return res
test_str = 'geeks4rgeeks'
print ( "The original string is : " + str (test_str))
res = alternate_reverse(test_str)
print ( "Is alternate reversed string : " + str (res))
|
Output
The original string is : geeks4rgeeks
Is alternate reversed string : kseergs4ekge
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5 : Using regular expressions
- Import the re module.
- Define a regular expression pattern that matches alternating groups of two characters in the string.
- Define a function that takes a match object as its argument and returns the reversed version of the match group.
- Use the re.sub() function to replace the matched groups in the string with their reversed version.
- Return the modified string.
Python3
import re
test_str = 'geeks4rgeeks'
print ( "The original string is : " + str (test_str))
pattern = r '(.)(.)'
def reverse_match(match_obj):
return match_obj.group( 2 ) + match_obj.group( 1 )
res = re.sub(pattern, reverse_match, test_str)
print ( "Is alternate reversed string : " + str (res))
|
Output
The original string is : geeks4rgeeks
Is alternate reversed string : egke4sgreesk
Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), as we need to create a new string to store the modified string.
Share your thoughts in the comments
Please Login to comment...