12

How do I reverse words in Python?

For instance:

SomeArray=('Python is the best programming language')
i=''
for x in SomeArray:
      #i dont know how to do it

print(i)

The result must be:

egaugnal gnimmargorp tseb eht si nohtyP

please help. And explain.
PS:
I can't use [::-1]. I know about this. I must do this in an interview, using only loops :)

Kevin
  • 74,910
  • 12
  • 133
  • 166
Vadim Kovrizhkin
  • 1,575
  • 4
  • 16
  • 27
  • 1
    If you manage to outsmart the interviewer by pretending you know absolute basic stuff where in fact you do not, what will happen if you get the job and will have to do real work? – Matthias Sep 18 '13 at 12:48
  • Haha, i didnt it:) and that's why i asked you:) – Vadim Kovrizhkin Sep 18 '13 at 12:59
  • 1
    @VadimKovrizhkin [Reverse a string in python without using `reversed` or `[::-1]`](http://stackoverflow.com/questions/18686860/reverse-a-string-in-python-without-using-reversed-or-1). – Ashwini Chaudhary Sep 18 '13 at 13:01
  • I did it like [::-1] And this answer didnt like them – Vadim Kovrizhkin Sep 18 '13 at 13:02
  • So basically, you're asking how to implement a low-level programming construct in a high-level language. I remember having to do this kind of stuff in a C++ class with pointers and all. The point of such an exercise is to know HOW these constructs work, and thus what kind of limitations, space- or time-wise, various options have. – Brian Peterson Sep 22 '13 at 02:31
  • From the duplicate question @hcwhsa linked to, http://stackoverflow.com/a/18686882/1151229 is my favorite answer. It uses recursion, and manages thereby not to be too long-winded. If you insisted on using iteration instead, the best answer is http://stackoverflow.com/a/18686861/1151229, provided by the asker himself. – Brian Peterson Sep 22 '13 at 03:06
  • But honestly if an interviewer asked me to illustrate this kind of low-level knowledge of iteration, I would just write them a Java program, a language much more suited to the task. – Brian Peterson Sep 22 '13 at 03:09

3 Answers3

20
>>> s = 'Python is the best programming language'
>>> s[::-1]
'egaugnal gnimmargorp tseb eht si nohtyP'

UPD:

if you need to do it in a loop, you can use range to go backwards:

>>> result = ""
>>> for i in xrange(len(s)-1, -1, -1):
...     result += s[i]
... 
>>> result
'egaugnal gnimmargorp tseb eht si nohtyP'

or, reversed():

>>> result = ""
>>> for i in reversed(s):
...     result += i
... 
>>> result
'egaugnal gnimmargorp tseb eht si nohtyP'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    No, I no about this [::-1] but on interview, i must did this without [::-1] just cycle. – Vadim Kovrizhkin Sep 18 '13 at 12:17
  • Do you want a reverse looping logic? – Vivek Sep 18 '13 at 12:25
  • @VadimKovrizhkin check updated answer, is it what you wanted? – alecxe Sep 18 '13 at 12:27
  • Can you do this without xrange? – Vadim Kovrizhkin Sep 18 '13 at 12:40
  • @VadimKovrizhkin why? Is this or requirement or it doesn't work for you? Then, may be you are on python 3 - use `range()` instead of `xrange()`. – alecxe Sep 18 '13 at 12:43
  • It was task on interview. I must do it without language ability (reverse, range, [::-1] and so on) – Vadim Kovrizhkin Sep 18 '13 at 12:45
  • For example, if i must find maximum element in array, i must do it without (max) just cycle: array =([1, 23, 4, 123, 4]) here=0 for x in array: if here < x: here=x print(here) – Vadim Kovrizhkin Sep 18 '13 at 12:48
  • @VadimKovrizhkin hey do you really need to reverse a string or the order of words inside? See http://stackoverflow.com/questions/1009160/reverse-the-ordering-of-words-in-a-string, http://stateofhouston.com/2011/02/23/stupid-interview-questions-everyone-still-asks-reverse-the-words-in-a-string/. – alecxe Sep 18 '13 at 13:03
  • array=('Python is the best programming language') here='' a=1 for x in array: here+=array[len(array)-a] a+=1 print(here) – Vadim Kovrizhkin Sep 20 '13 at 19:03
4

Use the slice notation:

>>> string = "Hello world."
>>> reversed_string = string[::-1]
>>> print reversed_string
.dlrow olleH

You can read more about the slice notatoin here.

Community
  • 1
  • 1
wmgaca
  • 41
  • 1
2

A string in Python is an array of chars, so you just have to traverse the array (string) backwards. You can easily do this like this:

"Python is the best programming language"[::-1]

This will return "egaugnal gnimmargorp tseb eht si nohtyP".

[::-1] traverses an array from end to start, one character at a time.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
zedutchgandalf
  • 233
  • 2
  • 6