Open In App

Reverse Alternate Characters in a String - Python

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Reversing alternate characters in a string involves rearranging the characters so that every second character is reversed while maintaining the original order of other characters. For example, given the string 'abcde', reversing the alternate characters results in 'ebcda', where the first, third and fifth characters ('a', 'c', 'e') are reversed and the second and fourth characters ('b', 'd') remain in place. Let's explore different methods to achieve this efficiently.

Using itertools.zip_longest

zip_longest() pair characters from two lists, ensuring that if one list is shorter, the remaining characters from the longer list are added without any errors. It’s like a smart assistant that makes sure no character is left behind.

Python
from itertools import zip_longest

s = 'abcde'
s1 = s[::2][::-1]
s2 = s[1::2]

res = ''.join(a + b if b else a for a, b in zip_longest(s1, s2))
print(res) 

Output
ebcda

Explanation:

  • s[::2][::-1] takes every second character starting from index 0 ('a', 'c', 'e') and reverses them to get s1 = 'eca'.
  • s[1::2] takes every second character starting from index 1 ('b', 'd') to get s2 = 'bd'.
  • zip_longest(s1, s2) pairs characters: 'e' with 'b', 'c' with 'd', and 'a' with None.
  • Inside join(), a + b if b else a merges the pairs, resulting in 'ebcda'.

Using list comprehension

This method creates a new list by combining characters in one clean line of code. It’s neat and concise, like writing a quick, efficient to-do list .

Python
s = 'abcde'
s1 = s[::2][::-1]
s2 = s[1::2]

res = [s1[i] + s2[i] if i < len(s2) else s1[i] for i in range(len(s1))]
if len(s2) > len(s1):
    res.append(s2[-1])

print(''.join(res))

Output
ebcda

Explanation:

  • s[::2][::-1] takes every second character from index 0 ('a', 'c', 'e'), reverses it to 'eca'.
  • s[1::2] takes every second character starting from index 1 ('b', 'd').
  • List comprehension pairs characters from s1 and s2, merging them ('e' + 'b', 'c' + 'd') and adding any leftover characters from s1.
  • If s2 is longer than s1, the remaining character(s) from s2 are appended, but in this case, it's skipped.

Using extend()

This method manually loops through both lists and pairs the characters one by one, adding them to a result list. It's like carefully picking out pieces and placing them in order. Afterward, it checks if there are any leftover characters and adds them.

Python
s = 'abcde'
s1 = list(s[::2])[::-1]
s2 = list(s[1::2])

res = []
for i in range(min(len(s1), len(s2))):
    res.extend([s1[i], s2[i]])

res.extend(s1[i+1:] + s2[i+1:])
print(''.join(res))

Output
ebcda

Explanation:

  • list(s[::2])[::-1] takes every second character from index 0 ('a', 'c', 'e'), then reverses the list to ['e', 'c', 'a'].
  • list(s[1::2]) takes every second character starting from index 1 ('b', 'd'), resulting in ['b', 'd'].
  • Loop through the shorter length (2 iterations), pairing characters from s1 and s2: ['e', 'b'] and ['c', 'd'], adding to res.
  • After the loop, add remaining characters from s1 ('a') to res, resulting in 'ebcda'.

Related articles


Next Article
Practice Tags :

Similar Reads