Python - Reversed Split Strings
Last Updated :
18 Jan, 2025
In Python, there are times where we need to split a given string into individual words and reverse the order of these words while preserving the order of characters within each word.
For example, given the input string "learn python with gfg", the desired output would be "gfg with python learn". Let's explore a few ways to achieve this efficiently in Python.
Using split() and reverse
This method involves breaking the string into a list of words using the split() function and then reversing the list in place using the reverse() method.
Python
s = "learn python with gfg"
# Break the string into words and reverse the list
words = s.split() # Split into words
words.reverse() # Reverse the list in place
print(words)
Output['gfg', 'with', 'python', 'learn']
Explanation:
- The split() function divides the string into a list of words based on spaces.
- The reverse() method changes the order of the elements in the list to reverse.
Let's explore some more methods and see how we can split a string into a list of words and then reverse the order.
Using split() with slicing
This approach uses slicing to reverse the list after breaking the string into words.
Python
s = "learn python with gfg"
# Break the string into words and reverse the list using slicing
words = s.split()[::-1] # Reverse the list using slicing
print(words)
Output['gfg', 'with', 'python', 'learn']
Explanation:
- split() function divides the string into words.
- Slicing with [::-1] creates a new list with elements in reverse order.
Using for loop
This method manually iterates over the string using a for loop, breaks it into words, and constructs the reversed list.
Python
s = "learn python with gfg"
# Initialize an empty list for reversed words
words = []
# Break the string into words and iterate in reverse order
for word in s.split()[::-1]:
words.append(word) # Append each word to the list
print(words)
Output['gfg', 'with', 'python', 'learn']
Explanation:
- split() function divides the string into words.
- The list is reversed using slicing, and words are added to a new list using a for loop.
Using list comprehension
This approach combines breaking, reversing, and collecting words in a list using a concise one-liner list comprehension.
Python
s = "learn python with gfg"
# Reverse the list using list comprehension
words = [word for word in s.split()[::-1]]
print(words)
Output['gfg', 'with', 'python', 'learn']
Explanation:
- split() function divides the string into words.
- Reversing is done using slicing with [::-1], and words are added to a new list with list comprehension.
Using reversed()
This method simply uses the reversed() function to reverse the list of words.
Python
s = "learn python with gfg"
# Break the string into words and reverse using reversed
words = list(reversed(s.split())) # Convert the reversed iterator to a list
print(words)
Output['gfg', 'with', 'python', 'learn']
Explanation:
- split() function creates a list of words.
- reversed() function creates a reversed iterator which is converted to a list.
Similar Reads
Reverse Sort a String - Python The goal is to take a given string and arrange its characters in descending order based on their Unicode values. For example, in the string "geeksforgeeks", the characters will be sorted from highest to lowest, resulting in a new string like "ssrokkggfeeeee". Let's understand different methods to pe
2 min read
Python - Reverse Slicing of given string Reverse slicing in Python is a way to access string elements in reverse order using negative steps.Using Slicing ([::-1])Using slicing with [::-1] in Python, we can reverse a string or list. This technique works by specifying a step of -1, which starts at the end of the sequence and moves backward,
1 min read
Python - Selectively Split in Strings Sometimes, while working with Python strings, we may have to perform a split. Not sometimes, normal one, depending on deliminator but something depending upon programming constructs like elements, numbers, words etc and segregate them. Lets discuss a way in which this task can be solved. Method : Us
3 min read
Python | Reverse Interval Slicing String Sometimes, while working with strings, we can have a problem in which we need to perform string slicing. In this, we can have a variant in which we need to perform reverse slicing that too interval. This kind of application can come in day-day programming. Let us discuss certain ways in which this t
4 min read
Python | Split by repeating substring Sometimes, while working with Python strings, we can have a problem in which we need to perform splitting. This can be of a custom nature. In this, we can have a split in which we need to split by all the repetitions. This can have applications in many domains. Let us discuss certain ways in which t
5 min read