In the previous article, we have discussed Python Program to Get the Position of Max Value in a List
Given a Sentence, the task is to Reverse each Word in a Given Sentence.
Split method:
Splitting a string into a list is accomplished by the split() method. It is a built-in Function in python.
join function:
The join() method is a string method that returns a string containing the elements of a sequence that have been joined by a string separator.
- Python Program to Remove the last Word from the String/Sentence
- Python Program to Find Longest Word from Sentence
- Python Program to Replace a Word with Asterisks in a Sentence
Examples:
Example 1:
Input:
Given String = Hello this is btechgeeks
Output:
The Reverse of each word of the above given sentence = olleH siht si skeeghcetb
Example 2:
Input:
Given String = Good Morning btechGeeks
Output:
The Reverse of each word of the above given sentence = dooG gninroM skeeGhcetb
Program to Reverse each Word in a Sentence
Below are the ways to Reverse each Word in a Sentence.
Method #1: Using split() Method (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Split the words of the given string to a list of words using the built-in split() function and store it in another variable.
- Traverse in the above-Given List of words using For Loop.
- Inside the Loop, Reverse each word of the list Using the Slicing method and store it in a variable.
- Join all the reversed words of a given sentence using the built-in join( ) function and store it in another variable.
- Print the reversed Sentence of each word of a given String.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. strng = "Hello this is btechgeeks" # Split the words of the given string to a list of words using the built-in split() function and store it in another variable. splt_wrds = strng.split() # Traverse in the above Given List of words using For Loop. # Inside the Loop, Reverse each word of the list Using Slicing method and # store it in a variable. rversd_wrds = [wrd[::-1] for wrd in splt_wrds] # Join all the reversed words of a given sentence using the built-in join( ) function # and store it in another variable. finl_sentnce = " ".join(rversd_wrds) # Print the reversed Sentence of each word of a given String. print("The Reverse of each word of the above given sentence = ", finl_sentnce)
Output:
The Reverse of each word of the above given sentence = olleH siht si skeeghcetb
Method #2: Using split() Method (User Input)
Approach:
- Give the string as User input using input() function and store it in a variable.
- Split the words of the given string to a list of words using the built-in split() function and store it in another variable.
- Traverse in the above-Given List of words using For Loop.
- Inside the Loop, Reverse each word of the list Using the Slicing method and store it in a variable.
- Join all the reversed words of a given sentence using the built-in join( ) function and store it in another variable.
- Print the reversed Sentence of each word of a given String.
- The Exit of the program.
Below is the implementation:
# Give the string as User input using input() function and store it in a variable. strng = input(" Enter some random String = ") # Split the words of the given string to a list of words using the built-in split() function and store it in another variable. splt_wrds = strng.split() # Traverse in the above Given List of words using For Loop. # Inside the Loop, Reverse each word of the list Using Slicing method and # store it in a variable. rversd_wrds = [wrd[::-1] for wrd in splt_wrds] # Join all the reversed words of a given sentence using the built-in join( ) function # and store it in another variable. finl_sentnce = " ".join(rversd_wrds) # Print the reversed Sentence of each word of a given String. print("The Reverse of each word of the above given sentence = ", finl_sentnce)
Output:
Enter some random String = Good Morning btechGeeks The Reverse of each word of the above given sentence = dooG gninroM skeeGhcetb
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
- Python Program to Find Longest Word from Sentence
- Python Program to Count Palindrome Words in a Sentence
- Python Program to Accept a Sentence and Print only the First Letter of Each Word in Capital Letters Separated by a Full Stop
- Python Program to Print each Word of a Sentence along with Number of Vowels in each Word