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.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Avijit Banerjee
  • 61
  • 1
  • 1
  • 4

4 Answers4

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'
agold
  • 6,140
  • 9
  • 38
  • 54
  • How can i count the total number of occurences of each letter in this program itself – Avijit Banerjee Dec 07 '15 at 08:47
  • That is actually a new question, but it is already answered before: [Count occurrence of a character in a string](http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – agold Dec 07 '15 at 08:52
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.

Tom Ron
  • 5,906
  • 3
  • 22
  • 38
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
Maxime Launois
  • 928
  • 6
  • 19
adityaB
  • 1
  • 1
  • 2
  • 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 08 '19 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)
Kevin C
  • 4,851
  • 8
  • 30
  • 64
SARAN SURYA
  • 534
  • 5
  • 14