Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Program to Reverse the sentence in python.

+4 votes
asked Feb 26, 2022 by Bipin Kumar (210 points)
closed Mar 5, 2022 by Admin

input='Hello how are you'

output='you are how Hello'

closed with the note: answered

3 Answers

+1 vote
answered Feb 27, 2022 by Peter Minarik (86,200 points)

This seems to be an assignment/homework. So you should really try to solve it yourself. I'll give you some idea of how you can tackle the problem, but you have to put in your own effort and write some code on your own.

Here are a few ideas on how you can solve the problem.

Classis solution: iterate backwards

You can write a loop that iterates from the end of the string (see the len() function) until the first character in the string (a variable goes from the length of the string until 0). In the loop, print each character after the other.

Slice

String and list slicing allow you to write a more compact code. So you could just tell Python that you want to slice your string where the slicing starts from the end and goes backwards.

Good luck!

If you get stuck, share your code written so far to get help on that.

commented Feb 27, 2022 by Bipin Kumar (210 points)
This question comes in one of my friends mind while practicing Python. If i start slicing from the end of the string it will reverse the string,('ouy era woh olleH") but that's not i actually  wanted as my output.
commented Feb 27, 2022 by Peter Minarik (86,200 points)
Oh, you're right. I was in a rush and I didn't pay full attention.

You wanted to keep the correct order of characters within the words, it's the words that are reversed. Ok. We can do that. :)

This is how I would approach the problem:

1. split the original string into words and create a list
2. iterate backwards in this list (from end to start) and print all the words

In a more "Pythonish" way you can write the whole thing in one line:

1. split the original string into words (split() method)
2. reverse your list (slice)
3. join your newly created array into a single string (join() method)

Good luck!
commented Feb 27, 2022 by Bipin Kumar (210 points)
It is working fine!..Thanks for your Help!
commented Feb 27, 2022 by Peter Minarik (86,200 points)
My pleasure.
0 votes
answered Feb 27, 2022 by Sreeja Chinthamreddy (140 points)
x=input().split(" ")

x.reverse()

print("".join(x))
+2 votes
answered Feb 28, 2022 by Shahriyor Yuldashev (690 points)
reshown Feb 28, 2022 by Shahriyor Yuldashev

You can use reversed() function:

for i in reversed(list(input().split())):
    print(i, end=' ')

commented Feb 28, 2022 by Bipin Kumar (210 points)
Yes it is working fine...Thanks a lot
commented Feb 28, 2022 by Shahriyor Yuldashev (690 points)
Welcome, my dear !
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...