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.
# 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)) |
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.
# 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)) |
The original string : Gfg is best The string after reverse split : best, is, Gfg
Recommended Posts:
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Split Sublist Strings
- Python | Ways to split strings using newline delimiter
- Python | Split CamelCase string to individual strings
- Python | Split strings in list with same prefix in all elements
- Python | Ways to split strings on Uppercase characters
- Python | Split list of strings into sublists based on length
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Python reversed() function
- Python | Reversed Order keys in dictionary
- Python String | split()
- Python | Remove empty strings from list of strings
- Python | Custom list split
- Python | Split tuple into groups of n
- Python | os.path.split() method
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.



