Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
# Alex Grattan
# 17/10/14

message = input("Enter your message to have it printed backwards:")
split_message = list(message.split())
reverse_message_list = []
reverse_message = ""

for i in range(len(split_message)):
    reverse_message_list.append(split_message.pop())

reverse_message_list = [x + ' ' for x in reverse_message_list]

for i in reverse_message_list:
    reverse_message += i

print (reverse_message)
share|improve this question

2 Answers 2

  • split returns a list already, so you don't need the list call
  • unless you really want to reimplement the reverse method, use it, or the function reversed if you want a return value instead
  • concatenating strings is much easier with " ".join(...)

So this can then be compressed to

# Alex Grattan
# 17/10/14

message = input("Enter your message to have it printed backwards:")
print (" ".join(reversed(message.split())))
share|improve this answer
    
Thanks! No I don't have a specific question. I am an autodidact, so I seek guidance and code reviews from others. –  AlexDGrattan Oct 17 '14 at 9:49
    
I wasn't aware the 'reverse' method existed - the novice I am. Google is my friend; I should have consulted her. –  AlexDGrattan Oct 17 '14 at 9:53
1  
Cool, I removed the question; also the standard library docs are pretty good if you're looking for something related to e.g. standard types. –  ferada Oct 17 '14 at 9:58

Why even go the route of for loops and calling join and reversed when you can just use extended slices?

message = input("Enter your message to have it printed backwards:")
print message[::-1]

the output would then be:

> Enter your message to have it printed backwards: 
    My name is Inigo Montoya, you have killed my father, prepare to die

eid ot eraperp ,rehtaf ym dellik evah uoy ,ayotnoM oginI si eman yM

EDIT: Per @ferada's comment, this reverses the sentence by words, not letters:

message = raw_input("Enter your message to have it printed backwards:")
print " ".join(message.split()[::-1])

and the output:

> Enter your message to have it printed backwards: 
    My name is Inigo Montoya, you have killed my father, prepare to die

die to prepare father, my killed have you Montoya, Inigo is name My

Also, do be mindful of the input()/raw_input() cross-compat issue with Python 2 and Python 3

share|improve this answer
    
Because the result is different? Splitting first will then just reverse the order of words, whereas this reverses the whole string. –  ferada Oct 17 '14 at 17:55
    
@ferada updated my answer to reflect reversing words using the extended slices. Similar to your answer, minus the call to reversed(). –  jsanc623 Oct 17 '14 at 19:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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