Open In App

Python program to reverse alternate characters in a string

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Alternate characters reverse in String
# Using loop + slicing + reversed()
 
# initializing string
test_str = 'geeks4rgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# extracting alternate string
alt = test_str[::2]
not_alt = test_str[1::2]
 
# performing reverse
alt = "".join(reversed(alt))
 
res = ''
# remaking string
for idx in range(len(alt)):
    res += alt[idx]
    res += not_alt[idx]
     
# printing result
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




# Python3 code to demonstrate working of
# Alternate characters reverse in String
# Using list comprehension
 
# initializing string
test_str = 'geeks4rgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using one-liner to solve the problem
res = "".join(["".join(reversed(test_str[::2]))[idx] + test_str[1::2][idx]
      for idx in range(len("".join(reversed(test_str[::2]))))])
     
# printing result
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'
 
# printing original string
print("The original string is : " + str(test_str))
 
# extracting alternate string
alt = test_str[::2]
not_alt = test_str[1::2]
 
# performing reverse
alt = "".join(reversed(alt))
 
# remaking string
res = "".join([x + y for x, y in zip(alt, not_alt)])
 
# printing result
print("Is alternate reversed string : " + str(res))
#This code is contributed by Vinay pinjala.


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




# Python3 code to demonstrate working of
# Alternate characters reverse in String
# Using loop and string concatenation.
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'
# printing original string
print("The original string is : " + str(test_str))
res=alternate_reverse(test_str)
# printing result
print("Is alternate reversed string : " + str(res))
#this code contributed by tvsk


Output

The original string is : geeks4rgeeks
Is alternate reversed string : kseergs4ekge

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #5 : Using regular expressions

  1. Import the re module.
  2. Define a regular expression pattern that matches alternating groups of two characters in the string.
  3. Define a function that takes a match object as its argument and returns the reversed version of the match group.
  4. Use the re.sub() function to replace the matched groups in the string with their reversed version.
  5. Return the modified string.

Python3




# Python3 code to demonstrate working of
# Alternate characters reverse in String
# Using regular expressions
 
# import the re module
import re
 
# initializing string
test_str = 'geeks4rgeeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# define a regular expression pattern that matches alternating groups of two characters
pattern = r'(.)(.)'
 
# define a function to reverse the matched group
def reverse_match(match_obj):
    return match_obj.group(2) + match_obj.group(1)
 
# use re.sub() function to replace the matched groups in the string with their reversed version
res = re.sub(pattern, reverse_match, test_str)
 
# printing result
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads