Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Reverse Interval Slicing String
Next article icon

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 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.

Table of Content

  • Using split() with slicing
  • Using for loop
  • Using list comprehension
  • Using reversed()

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.

Next Article
Python | Reverse Interval Slicing String
author
manjeet_04
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 | Split Sublist Strings
    Yet another variation of splitting strings is splitting the strings that are an element of the sublist. This is quite a peculiar problem, but one can get the data in this format, and the knowledge of splitting it anyways is quite useful. Let's discuss certain ways in which this particular task can b
    5 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 th
    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 t
    2 min read
  • Python | Rear stray character String split
    C/C++ Code # 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 co
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences