4

E.g:

 input:  I live in New York
 output: York New in live I

P.S: I have used s[::-1], this just reverses the string, like kroY weN ni evil I, but this is not the desired output. I also tried :

def rev(x) :
    x = x[::-1]
    for i in range(len(x)) :
        if x[i] == " " :
            x = x[::-1]
            continue
        print x

But this also stands incorrect. Kindly help me in writing the code.

4 Answers 4

12

You can use split to get the separate words, reverse to reverse the list, and finally join to join them again to make the final string:

s = "This is New York"
# split first
a = s.split()
# reverse list
a.reverse()
# now join them
result = " ".join(a)
# print it
print(result)

results in:

'York New is This'
2
8
my_string = "I live in New York"
reversed_string = " ".join(my_string.split(" ")[::-1])

This is a 3 phase process - First we split the string to words, then we reverse the words and then connect them together again.

3
  • Nicely explained... this is also w/o using third variable too. Dec 7, 2015 at 8:39
  • How can i count the total number of occurences of each letter in this program itself Dec 7, 2015 at 8:47
  • For the clean way of doing it +1 since I had to reverse two strings and match them to remove the last occurrence of the word
    – iam.Carrot
    Jan 21, 2018 at 17:26
0

1st method

You need to split the given string, so that what ever words you've given in the string will be saved as the list data type. Then you can reverse the list elements and join it with spaces.

x = input("Enter any sentence:")
y = x.split(' ')
r = y[::-1]
z = ' '.join(r)

print(z)

2nd method

Same as first one but after reversing you need to iterate over the list and print elements by inserting the empty space (" ") after each list element.

x = input("Enter any sentence: ")
y = x.split(' ')
r = y[::-1]

for i in r:
    print(i , end=" ")

Examples

  • Input: I live in New York
  • Output: York New in live I
1
  • 1
    1.u need to split the given any kind of string ,so that wat ever words u've given in the string will be saved as the list data type .then u can reverse the list elements and join it with space ( ' ') . then for sure u will get the words reversed in the given string Example:- input: I live in New York output: York New in live I 2.Same as first one but after reversing u need to iterate the loop in the given number of list in the sequences with connecting the empty space " " between each list element. Example:- input: I live in New York output: York New in live I
    – adityaB
    Jun 8, 2019 at 17:38
0

this can be another method, but this works:

a="this is new york"
b=a.split(" ")
tem=[]
i=-1
for j in range(len(b)):
   tem.append(b[i])
   i-=1
print(*tem)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.