Concantenation and string slicing

D

DannyB

I've written a program that takes a phrase and spits it back out
backwards. My problem is it throws each character on a new line. I'd
like the phrase to be on the same line. Is this possible?

#Backward Message

message = raw_input("Enter a message: ")
letter = len(message)
while (letter > 0):
newMessage = ""
newMessage += message[letter-1]
letter -= 1
print newMessage


Thanks!!

Dan
 
C

CatDude

I've written a program that takes a phrase and spits it back out
backwards. My problem is it throws each character on a new line. I'd
like the phrase to be on the same line. Is this possible?

First suggestion: Put a comma at the end of the "print" line:
 
L

Larry Bates

DannyB said:
I've written a program that takes a phrase and spits it back out
backwards. My problem is it throws each character on a new line. I'd
like the phrase to be on the same line. Is this possible?

#Backward Message

message = raw_input("Enter a message: ")
letter = len(message)
while (letter > 0):
newMessage = ""
newMessage += message[letter-1]
letter -= 1
print newMessage


Thanks!!

Dan
Better was is:

message = raw_input("Enter a message: ")
print message[::-1]

-Larry Bates
 
D

Dennis Lee Bieber

I've written a program that takes a phrase and spits it back out
backwards. My problem is it throws each character on a new line. I'd
like the phrase to be on the same line. Is this possible?

#Backward Message

message = raw_input("Enter a message: ")
letter = len(message)
while (letter > 0):
newMessage = ""
This line is making a blank "newMessage" each time you process a
character
newMessage += message[letter-1]
You've now created a "newMessage" with one letter in it
letter -= 1
print newMessage
You've now printed that one-character message as the total output


While inefficient, what you want is:

message = raw_input("Enter a message: ")
letter = len(message)
newMessage = ""
while letter > 0:
newMessage += message[letter-1]
letter -= 1
print newMessage

Notice where the initialization of newMessage, and the print
statement, are located.
--
 
D

Dennis Lee Bieber

Better was is:

message = raw_input("Enter a message: ")
print message[::-1]

I sometimes get the feeling a lot of responses to newbies, lately,
fail to explain what was wrong in the original code in favor of showing
off the tricks that Python is capable of...
--
 
L

Larry Bates

Dennis said:
Better was is:

message = raw_input("Enter a message: ")
print message[::-1]

I sometimes get the feeling a lot of responses to newbies, lately,
fail to explain what was wrong in the original code in favor of showing
off the tricks that Python is capable of...

Several others had already pointed out what was wrong (which
of course the OP would have know if he had done a tutorial).
I merely wanted to post a solution that IMHO was a much
better way to accomplish the task. The OP is free to choose
among the posted solutions. If others had not yet posted the
solution, I would have posted both.

-Larry Bates
 
D

Dennis Lee Bieber

Several others had already pointed out what was wrong (which
of course the OP would have know if he had done a tutorial).

Actually, I think most of the other answers focused on the addition
of a "," to make the print stay on one line... But they all overlooked
that the entire loop was not, as stated, "reversing" the string itself.
<G>


My comment was meant to be general -- not directed to anyone in
particular; I've noticed a few in the last fiew days which seem to go
from "here is my code, why doesn't it work like..." to "alternate
solution" without ever once explaining what was wrong with the original
algorithm (in this example: clearing the "accumulative string" and
printing on /each/ iteration, which was /not/ reversing a string and
then printing it.)
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top