1
\$\begingroup\$
# 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)
\$\endgroup\$
5
\$\begingroup\$
  • 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())))
\$\endgroup\$
  • \$\begingroup\$ Thanks! No I don't have a specific question. I am an autodidact, so I seek guidance and code reviews from others. \$\endgroup\$ – AlexDGrattan Oct 17 '14 at 9:49
  • \$\begingroup\$ I wasn't aware the 'reverse' method existed - the novice I am. Google is my friend; I should have consulted her. \$\endgroup\$ – AlexDGrattan Oct 17 '14 at 9:53
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – ferada Oct 17 '14 at 9:58
2
\$\begingroup\$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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