Skip to content

chauhankashmira/Python-ReverseWords

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Reverse Words in a Sentence – Python Script

This simple Python script demonstrates how to reverse the order of words in a sentence using basic string and list operations.

πŸ“ Description

The script takes a string, splits it into a list of words, reverses the order of the words, and then joins them back into a single string. This is a common exercise to demonstrate string manipulation and list handling in Python.

πŸ“œ Code Overview

str = "Welcome to Python Programming Language"

# Split the sentence into a list of words
words = str.split(" ")
print(words)
# Output: ['Welcome', 'to', 'Python', 'Programming', 'Language']

# Reverse the list of words
words = words[-1::-1]
print(words)
# Output: ['Language', 'Programming', 'Python', 'to', 'Welcome']

# Join the reversed list into a single string
outputstr = " ".join(words)
print(outputstr)
# Output: Language Programming Python to Welcome

βœ… Output

['Welcome', 'to', 'Python', 'Programming', 'Language']
['Language', 'Programming', 'Python', 'to', 'Welcome']
Language Programming Python to Welcome

πŸ’‘ How It Works

  1. split(" "): Splits the sentence into words based on spaces.
  2. [::-1]: Reverses the list of words.
  3. join(): Concatenates the reversed words back into a sentence.

πŸš€ Usage

To run this script:

  1. Make sure you have Python installed (version 3.x).
  2. Copy the code into a .py file, e.g., reverseWords.py.
  3. Run the file using:
python reverseWords.py

πŸ“‚ Applications

  • Natural language processing (NLP)
  • Data cleaning and formatting
  • Interview questions and programming exercises

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages