
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to reverse each word in a sentence?
Here we use python built in function. First we split the sentence into a list of word. Then reverse each word and creating a new list ,here we use python list comprehension technique and last joining the new list of words and create an new sentence.
Example
Input :: PYTHON PROGRAM Output :: NOHTYP MARGORP
Algorithm
Step 1 : input a sentence. And store this in a variable s. Step 2 : Then splitting the sentence into a list of words. w=s.split(“”) Step 3 : Reversing each word and creating a new list of words nw. Step 4 : Joining the new list of words and make a new sentence ns.
Example Code
# Reverse each word of a Sentence # Function to Reverse words def reverseword(s): w = s.split(" ") # Splitting the Sentence into list of words. # reversing each word and creating a new list of words # apply List Comprehension Technique nw = [i[::-1] for i in w] # Join the new list of words to for a new Sentence ns = " ".join(nw) return ns # Driver's Code s = input("ENTER A SENTENCE PROPERLY ::") print(reverseword(s))
Output
ENTER A SENTENCE PROPERLY :: PYTHON PROGRAM NOHTYP MARGORP
- Related Articles
- Java program to reverse each word in a sentence
- Write a java program to reverse each word in string?
- Python Program to replace a word with asterisks in a sentence
- Program to reverse the position of each word of a given string in Python
- Java program to count the characters in each word in a given sentence
- Python program to remove all duplicates word from a given sentence.
- Write a java program reverse tOGGLE each word in the string?
- C++ program to Reverse a Sentence Using Recursion
- Java Program to Reverse a Sentence Using Recursion
- Haskell Program to Reverse a Sentence Using Recursion
- Golang Program to Reverse a Sentence using Recursion
- Reverse each word in a linked list Node
- Java Program to replace a word with asterisks in a sentence
- PHP program to find the first word of a sentence
- Print longest palindrome word in a sentence in C Program

Advertisements