The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Help with concatenating
Discuss Help with concatenating in the Python Programming forum on Dev Shed. Help with concatenating Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 18th, 2004, 05:35 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Help with concatenating
How do I concatenate or get the out put to print on the same line? I know I need a "+," but it's not working where I think it should.
message = raw_input("Please enter a message to be printed backwards. ")
print
i = 1
while i <= len(message):
new_message = message[-i]
i = i + 1
print new_message
raw_input("\n\nPress the enter key to exit.")
|

February 18th, 2004, 05:48 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
Quote: | Originally Posted by briangw How do I concatenate or get the out put to print on the same line? I know I need a "+," but it's not working where I think it should.
message = raw_input("Please enter a message to be printed backwards. ")
print
i = 1
while i <= len(message):
new_message = message[-i]
i = i + 1
print new_message
raw_input("\n\nPress the enter key to exit.") |
Well, there are several ways. An easy way is to put a comma after what you're printing:
Code:
print "This is some text",
This also works great for printing multiple objects of varying type:
Code:
print 1, "is greater than", 0, "but less than", 8
The print with comma syntax will always put a space between your items.
If you want more control on exactly what you are printing, I suggest you read what Mark Pilgrim has to say in Dive Into Python.
Last edited by percivall : February 18th, 2004 at 05:54 PM.
|

February 18th, 2004, 06:04 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Canada
|
|
First of all, you need to indent (four spaces) where it says:
new_message = message[-i]
You're using the plus sign in the proper way. I think I know why your script is not working, though.
Take a look at this:
Code:
01: message = raw_input("Please enter a message to be printed backwards. ")
02: print
03:
04: i = 1
05:
06: while i <= len(message):
07: new_message = message[-i]
08: i = i + 1
09: print new_message
10:
11: raw_input("\n\nPress the enter key to exit.")
Okay, look on line four. Now look on line eight. See the problem yet?
You defined i as 1. You then definied i as i + 1. It's going to use the latest definition, and will forget about the i = 1. I would use:
i = 1
i2 = i + 1
Do you understand what I'm saying?
|

February 18th, 2004, 06:08 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I see now. indenting is something I still have not fully grasped. Created a ton of loops with not doing it properly. Thanks for the input. I see the error of my ways! 
|

February 18th, 2004, 06:13 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by MasterChief First of all, you need to indent (four spaces) where it says:
new_message = message[-i]
You're using the plus sign in the proper way. I think I know why your script is not working, though.
Take a look at this:
Code:
01: message = raw_input("Please enter a message to be printed backwards. ")
02: print
03:
04: i = 1
05:
06: while i <= len(message):
07: new_message = message[-i]
08: i = i + 1
09: print new_message
10:
11: raw_input("\n\nPress the enter key to exit.")
Okay, look on line four. Now look on line eight. See the problem yet?
You defined i as 1. You then definied i as i + 1. It's going to use the latest definition, and will forget about the i = 1. I would use:
i = 1
i2 = i + 1
Do you understand what I'm saying? |
I tried what you said, but it locked up Python. I rebooted and it did the same thing. Here's my modified code:
message = raw_input("Please enter a message to be printed backwards. ")
print
i = 1
while i <= len(message):
new_message = message[-i]
i2 = i + 1
print new_message
raw_input("\n\nPress the enter key to exit.")
Isn't the problem with changing the "i" variable to "i2?"
Sorry, the formatting here isn't showing right. I do have the new_message line indented 4 spaces.
Last edited by briangw : February 18th, 2004 at 06:17 PM.
|

February 18th, 2004, 06:29 PM
|
|
Contributing User
|
|
Join Date: Feb 2003
Location: Canada
|
|
|
Hmmm... I'll have to play around with it a bit. It was locking up on me with the original script and the modified script.
|

February 18th, 2004, 06:43 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
after putting the comma in at the end of the print statement, it worked.
what's interesting is that my professor so far has said that he has seen many different ways (and code) to come to the same conclusion.
|

February 18th, 2004, 07:07 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
The reason you're code was locking Python up is because 'i' could never be anything but one so the loop could never end...
So all you really needed here was the comma GW  . But wait, that leaves a space between each letter which isn't very pretty. But we can use the backspace char (\b) to remove it.
Code:
#!/usr/bin/env python
message = raw_input("Please enter a message to be printed backwards. ")
print
i = 1
while i <= len(message):
new_message = message[-i]
i = i + 1
print '\b' + new_message,
raw_input("\n\nPress the enter key to exit.")
Of the same thing could be done like this in Python 2.3 using extended slices.
Code:
>>> print raw_input('Enter the word to be Reversed: ')[::-1]
Enter the word to be Reversed: word
drow
>>>
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : February 18th, 2004 at 07:10 PM.
|

February 18th, 2004, 07:17 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
No good. He's demanding a "+" in there. He sent me to a chapter on "building a new string" by rebuilding the word, but backwards with no spaces.
My problem is is that I'm confused with how the book is explaining it
Last edited by briangw : February 18th, 2004 at 07:24 PM.
|

February 18th, 2004, 07:53 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Very simple answer, loop over the string in reverse and concatenate each letter to a new string.. leaving you with a string and all using the + operator.
Code:
>>> string = raw_input('Enter the string to be reversed: ')
>>> output = ''
>>> for letter in string[::-1]:
... output = output + letter
...
>>> print output
gnirts
>>>
If you're not aloud to use extended slices then you'll neet to loop over the number of letters (for each in range(len(string))... or... for index, value in enumerate(string)) and offset the index number by the strings len(). If you want an example let me know?
Also, do you have a link to this book, maybe i can help you understand it  .
Mark.
|

February 18th, 2004, 08:06 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by netytan Very simple answer, loop over the string in reverse and concatenate each letter to a new string.. leaving you with a string and all using the + operator.
Code:
>>> string = raw_input('Enter the string to be reversed: ')
>>> output = ''
>>> for letter in string[::-1]:
... output = output + letter
...
>>> print output
gnirts
>>>
If you're not aloud to use extended slices then you'll neet to loop over the number of letters (for each in range(len(string))... or... for index, value in enumerate(string)) and offset the index number by the strings len(). If you want an example let me know?
Also, do you have a link to this book, maybe i can help you understand it  .
Mark. |
Python Programming for the Absolute Beginner, by Michael Dawson, 1-59200-073-8
I tried your code, but got error:
Traceback (most recent call last):
File "C:/Documents and Settings/Brian/Desktop/PYTHON/Class 5/test", line 9, in ?
for letter in message[::-1]:
TypeError: sequence index must be integer
|

February 18th, 2004, 09:28 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 17
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Got it!
message = raw_input("Please enter a message to be printed backwards. ")
new_message = ""
for letter in message:
****new_message = letter + new_message
print new_message
raw_input("\n\nPress the enter key to exit.")
I hate programming. It's required for my network admin degree. Thanks for your help. Midterms are coming up next week and we have some programs to write, so I may be calling on some help again.
Thanks!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|