Python - Reversed Split Strings Last Updated : 18 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 reverseThis 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.Table of ContentUsing split() with slicingUsing for loopUsing list comprehensionUsing reversed()Using split() with slicingThis 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 loopThis 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 comprehensionThis 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. Comment More infoAdvertise with us Next Article Python - Reversed Split Strings manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python string-programs Practice Tags : python 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 Python | Reverse Incremental String Slicing Sometimes, while working with Python strings, we can have a problem in which we need to perform the slice and print of strings in reverse order. This can have applications in day-day programming. Let us discuss certain ways in which this task can be performed. Method #1: Using loops This is the brut 4 min read Python - String Split including spaces String splitting, including spaces refers to breaking a string into parts while keeping spaces as separate elements in the result.Using regular expressions (Most Efficient)re.split() function allows us to split a string based on a custom pattern. We can use it to split the string while capturing the 3 min read Reverse All Strings in String List in Python We are given a list of strings and our task is to reverse each string in the list while keeping the order of the list itself unchanged. For example, if we have a list like this: ['gfg', 'is', 'best'] then the output will be ['gfg', 'si', 'tseb'].Using For LoopWe can use a for loop to iterate over th 2 min read Python | Rear stray character String split Python3 # Python3 code to demonstrate working of # Rear stray character String split # Using list comprehension # initializing string test_str = 'gfg, is, best, ' # printing original string print("The original string is : " + test_str) # Rear stray character String split # Using list compr 5 min read Python - Reverse Range in String List Given a string list, reverse each element of string list from ith to jth index. Input : test_list = ["Geeksforgeeks", "Best", "Geeks"], i, j = 1, 2 Output : ['ee', 'es', 'ee'] Explanation : Range of strings are extracted. Input : test_list = ["Geeksforgeeks"], i, j = 1, 7 Output : ['eeksfor'] Explan 3 min read Like