Python | Reversed Split Strings

The functionality of splitting is quite popular with manyfold applications and usages. With all, comes the scopes of many types of variations. This article discusses one among those variations in which one desires to get the split and reversal of element string order, both the operations at once. Let’s discuss certain ways to solve this particular problem.

Method #1 : Using join() + reversed() + split()

In this particular method, we first get the element words using the split function, perform the reversal of order of them using the reversed function and then perform the join to bind together the elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Reverse string split
# using join() + reversed() + split()
  
# initializing string
test_string = "Gfg is best"
  
# printing original string 
print("The original string : " + str(test_string))
  
# using join() + reversed() + split()
# Reverse string split
res =  ", ".join(reversed(test_string.split(" ")))
  
# print result
print("The string after reverse split : " + str(res))

chevron_right


Output :

The original string : Gfg is best
The string after reverse split : best, is, Gfg

 

Method #2 : Using join() + split() + list slicing

This method is similar to the above method in which we perform split and join, but the only difference in this method is that we use list slicing to perform the reversal.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Reverse string split
# using join() + split() + list slicing
  
# initializing string
test_string = "Gfg is best"
  
# printing original string 
print("The original string : " + str(test_string))
  
# using join() + split() + list slicing
# Reverse string split
res =  ', '.join(test_string.split()[::-1])
  
# print result
print("The string after reverse split : " + str(res))

chevron_right


Output :

The original string : Gfg is best
The string after reverse split : best, is, Gfg


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.