GeeksforGeeks App
Open App
Browser
Continue

# Python program to reverse alternate characters in a string

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 stringtest_str = 'geeks4rgeeks' # printing original stringprint("The original string is : " + str(test_str)) # extracting alternate stringalt = test_str[::2]not_alt = test_str[1::2] # performing reversealt = "".join(reversed(alt)) res = ''# remaking stringfor idx in range(len(alt)):    res += alt[idx]    res += not_alt[idx]     # printing resultprint("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 stringtest_str = 'geeks4rgeeks' # printing original stringprint("The original string is : " + str(test_str)) # using one-liner to solve the problemres = "".join(["".join(reversed(test_str[::2]))[idx] + test_str[1::2][idx]      for idx in range(len("".join(reversed(test_str[::2]))))])     # printing resultprint("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 stringprint("The original string is : " + str(test_str)) # extracting alternate stringalt = test_str[::2]not_alt = test_str[1::2] # performing reversealt = "".join(reversed(alt)) # remaking stringres = "".join([x + y for x, y in zip(alt, not_alt)]) # printing resultprint("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 stringprint("The original string is : " + str(test_str))res=alternate_reverse(test_str)# printing resultprint("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 moduleimport re # initializing stringtest_str = 'geeks4rgeeks' # printing original stringprint("The original string is : " + str(test_str)) # define a regular expression pattern that matches alternating groups of two characterspattern = r'(.)(.)' # define a function to reverse the matched groupdef 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 versionres = re.sub(pattern, reverse_match, test_str) # printing resultprint("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.

My Personal Notes arrow_drop_up