0

I tried doing this

while 1:
    line = input('Line: ')
    print(line[::-1])

but all that did was reverse the whole sentence, I was wondering if someone could help me with a program that converts 'hello world' to 'olleh dlrow' instead of 'dlrow olleh', and how do I make a loop that stops when the input is nothing, or just a space? Thank you in advanced!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Samir
  • 2,899
  • 4
  • 18
  • 19

1 Answers1

5

You need to split the sentence, reverse the words, then reassemble.

The simplest way to split is to do so on whitespace, with str.split(); reassembly is then just a case of re-joining the reversed words with a space:

' '.join([word[::-1] for word in line.split()])

Demo:

>>> line = 'hello world'
>>> ' '.join([word[::-1] for word in line.split()])
'olleh dlrow'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank You so much! And for the while loop, if there is nothing in the input, does it just go to the next command? – Samir Aug 06 '13 at 11:15
  • If `line` is an empty string, splitting returns a list with one empty string as the contents, so this code reverses the empty string, then rejoins that, resulting in an empty string. :-) – Martijn Pieters Aug 06 '13 at 11:16
  • If you want to skip empty lines, just test for them; `if not line: continue`. – Martijn Pieters Aug 06 '13 at 11:17
  • And let's say I want to write all my 'lines' and when I have no more to write, I press enter when asked for another line, and it will print all the reversed text, how do I go about doing that? – Samir Aug 06 '13 at 11:18
  • @Samir: You'd collect all those lines into a list object, then loop over the lines in the list object and apply my answer to each before printing. – Martijn Pieters Aug 06 '13 at 11:25
  • May I ask how that is done? Thank you :) – Samir Aug 06 '13 at 11:26
  • You need to do *some* work yourself. :-) – Martijn Pieters Aug 06 '13 at 11:28
  • @MartijnPieters: I am sure you know it. The outer square brackets can be safely removed for the same functionality (i.e. generator expression instead of the generated list). – pepr Aug 06 '13 at 11:34
  • 1
    @pepr: Using a list comprehension in `str.join()` is faster than a generator expression, because `str.join()` will turn the generator expression into a list *anyway*; it needs to loop over the sequence twice to calculate the output size first, then build the string. A list comprehension beats out a generator in speed tests here. – Martijn Pieters Aug 06 '13 at 11:35
  • 'line = input('Line: ') while line != '': list1 = [] line = input('Line: ') apend = ' '.join(word[::-1] for word in line.split()) list1.append(line) print(list1)' I'm pretty sure this is wrong, because it doesn't work, and i don't know how to fix it, any tips? – Samir Aug 06 '13 at 11:41
  • @Samir: You want to put your `list1 = []` **outside** your while loop, because otherwise you are replacing it each time there's a new line. – Martijn Pieters Aug 06 '13 at 11:41
  • line = input('Line: ') list1 = [] while line != '': line = input('Line: ') list1.append(line) print(' '.join(word[::-1] for word in line.split())) I still don't know how to fix it, am I blind, is the correct answer right in front of me. – Samir Aug 06 '13 at 11:46
  • Why don't you post a new question with that problem? – Martijn Pieters Aug 06 '13 at 12:49
  • 1
    @pepr: See [list comprehension without \[ \], Python](http://stackoverflow.com/q/9060653) – Martijn Pieters Aug 06 '13 at 12:50