In this program, we will learn how to reverse the words in a given string.
To understand this, you must have knowledge of the following topics in python:-
- String
- Loops
- Split and join function
- Step Argument
Program 1(By using a while loop)
str= "I like to read"
print("The given string is:",str)
wordlist = str.split()
lastindex = len(wordlist) - 1
newStr = ''
i = lastindex
while(i >= 0):
newStr = newStr + wordlist[i]
newStr = newStr + ' '
i = i - 1
print("The reversed form is: ", newStr)
Output:
The given string is: I like to read
The reversed form is: read to like I
Explanation:
- We have stored a string “I like to read” in a variable named str.
- Next We have splitted the string and stored the parts in a variable named as wordlist. This would essentially be a list, as the split function splits a string and create a list containing the splitted parts. This would be the list – [“I”, “like”, “to”, “read”]
- Next, we need the last index of the list. So we have subtracted 1 from the list length and stored it in a variable named ‘lastindex’. Note that we have used the len function for this.
- We have declared another variable named newStr and assigned it an empty string. This variable will hold the result at the end.
- Now, We need to fetch the items (strating from the last) from the list and append them to the variable ‘newStr’. For this, we need to run a reverse loop starting from the last index of the list to 0.
- The while loop is executing for i >= 0. Where i contains the value of lastindex (which in our case is 3, as the total number of elements in the list are 4).
- With each iteration we are appending the element(word) that stays at the current index to the variable newStr, and appends a space right after appending the word.
- At the end of the loop, we are printing the reversed string.
Program 2(By using the join function)
str= “I like to read”
print("The given string is:",str)
words=str.split(" ")
words=words[::-1]
result= " ".join(words)
print("The reversed form is:",result)
Output:
The given string is: I like to read
The reversed form is: read to like I
Program 3(Taking input from the user):
str=input(“Enter string of your choice ”)
print("The given string is:",str)
words=str.split(" ")
words=words[::-1]
result= " ".join(words)
print("The reversed form is:", result)
Output:
Enter the string of your choice I like to read
The given string is: I like to read
The reversed form is: read to like I
Explanation:-
- First of all, we have taken a string and stored it in a variable named str.
- Then, we’ve printed the string as it is, using the print function.
- In the third statement, we have used the split function. Here, the split function is changing the string into a list containing all the words of the string as the elements of the list. Now, we have a list containing all the words of the string.
- In the next statement, we have used step argument to reverse the order of the list. After doing this, we get a list containing the elements in the reversed order.
- Lastly, we have joined the obtained list with the help of the “sort” function and separated it with space.
- Finally we have printed the result using the print function.