473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Concantenation and string slicing

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

Feb 23 '06 #1
5 1124
nak
add a dash like shown below
print newMessage,


Feb 23 '06 #2
Wow - thats simple =). Thanks a ton!!

Feb 23 '06 #3
On Thu, 23 Feb 2006 14:55:16 -0800, DannyB wrote:
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:
print newMessage,

Feb 24 '06 #4
DannyB wrote:
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
Feb 24 '06 #5
Dennis Lee Bieber wrote:
On Thu, 23 Feb 2006 18:06:46 -0600, Larry Bates
<la*********@websafe.com> declaimed the following in comp.lang.python:

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
Feb 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ofer h | last post by:
hello all i have got a span inside a td of a table. i am trying to clip the string that exceeds the length of the td and then put three dots ("...") after it so it will show that the text was...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
3
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
10
by: rtilley | last post by:
s = list('some_random_string') print s s.reverse() print s s = ''.join(s) print s Surely there's a better way to do this, right?
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
16
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it...
2
by: Rahul | last post by:
Hi Everyone, I was working around object slicing (pass by value) and was wondering how it is specified in the standard, class A { }; class B: public A
0
by: Henry Chang | last post by:
Hi everyone, Suppose I start out with a raw string of utf-8 code points. raw_string = "68656E727963" I can coerce it into proper unicode format by slicing out two characters at a time. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.