We will develop a program to reverse each word in a string python. In this article, we are using the split() function, list comprehension, and join() function to reverse each word of a string in Python. The split() method splits the string from the specified separator and returns a list object with string elements. The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator. List comprehensions provide a concise way to create lists.
Example of reverse each word in a string:-
String: know program is best to learn programming
Reverse: wonk margorp si tseb ot nrael gnimmargorp
Reverse Each Word of a String in Python
We will take a string while declaring the variables. Then, find the reverse of each word in a string using the for loop. Finally, the result will be displayed on the screen.
# Python program to reverse each word in a string
# take inputs
string = 'Know Program'
# splitting the string into list of words
words = string.split(' ')
# reversing each word and creating a new list of words
reverseWords = [word[::-1] for word in words]
# joining the new list of words to for a new string
reverseString = " ".join(reverseWords)
# print reverse of each word in a string
print('The reverse is', reverseString)
Output:-
The reverse is wonK margorP
Python Program to Reverse Each Word in a String
In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.
# Python program to reverse each word in a string
# take inputs
string = input('Enter the string: ')
# splitting the string into list of words
words = string.split(' ')
# reversing each word and creating a new list of words
reverseWords = [word[::-1] for word in words]
# joining the new list of words to for a new string
reverseString = " ".join(reverseWords)
# print reverse of each word in a string
print('The reverse is', reverseString)
Output for the input values test-case-1:-
Enter the string: Welcome to Know Program
The reverse is emocleW ot wonK margorP
Output for the input values test-case-2:-
Enter the string: reverse each word in a string python
The reverse is esrever hcae drow ni a gnirts nohtyp
Reversing Each Word in a String in Python
This method is similar to the above method, but rather a shorthand method. In this program, reverse each word of the string in one line.
# Python program to reverse each word in a string
# take inputs
string = input('Enter the string: ')
# reverse each word in a string
reverseString = " ".join(word[::-1] for word in string.split(" "))
# print reverse of each word in a string
print('The reverse is', reverseString)
Output:-
Enter the string: Python Program
The reverse is nohtyP margorP
Reverse Each Word in a String Python
We can also take the help of a function to reverse each word in a string. A function is a block of code that performs a specific task.
# Python program to reverse each word in a string
def reverseWords(s): #user-defined function
return " ".join(word[::-1] for word in s.split(" "))
# take inputs
string = input('Enter the string: ')
# calling function and display result
print('The reverse is', reverseWords(string))
Output for the input values test-case-1:-
Enter the string: My name is guddu kumar singh
The reverse is yM eman si uddug ramuk hgnis
Output for the input values test-case-2:-
Enter the string: know program is best to learn programming
The reverse is wonk margorp si tseb ot nrael gnimmargorp
Also See:- Python Program to Reverse a String