Python | Consecutive element swapping in String

Sometimes, while working with strings, we can have a problem in which we may require to perform swapping of consecutive elements in string. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using join() + zip() + generator expression
The combination of above functions can be used to solve this problem. In this, we perform the task of joining consecutive characters using zip() and generator expression is used to provide the swap logic.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Consecutive element swapping in String
# using join() + zip() + generator expression
  
# initializing string 
test_str = "gfgisbesty"
  
# printing original string 
print("The original string is : " + test_str)
  
# Consecutive element swapping in String
# using join() + zip() + generator expression
res = ''.join([char[1] + char[0] for char in zip(test_str[::2], test_str[1::2])])
  
# printing result
print("String after Consecutive Swapping : " + str(res))

chevron_right


Output :



The original string is : gfgisbesty
String after Consecutive Swapping : fgigbsseyt

 

Method #2 : Using regex expression
This task can also be performed using regular expression using correctly framed regex.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Consecutive element swapping in String
# using regular expression
import re 
  
# initializing string 
test_str = "gfgisbesty"
  
# printing original string 
print("The original string is : " + test_str)
  
# Consecutive element swapping in String
# using regular expression
res = re.sub(r'(.)(.)', r'\2\1', test_str)
  
# printing result
print("String after Consecutive Swapping : " + str(res))

chevron_right


Output :

The original string is : gfgisbesty
String after Consecutive Swapping : fgigbsseyt


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.