Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 26th, 2006, 8:48 PM   #1
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
print problem

When I print something in python with a for loop it automatically skips a line after each print, like this:

0
1
2
3
4
5

How do I keep it from skipping a line so it outputs like this: 012345?

thanks
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Sep 26th, 2006, 8:53 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Investigate the comma. That may still not suit you, so look into formatted printing. There is a ton of information on it in the Python docs. You could, of course, simply concatenate prior to the print.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 27th, 2006, 6:25 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
As DaWei says, you can use a trailing comma:
python Syntax (Toggle Plain Text)
  1. for i in range(6):
  2. print i,
  3. print
  4. # prints: 0 1 2 3 4 5
Although this has the disadvantage of placing a space between each number. Or you could concatenate the numbers together:
python Syntax (Toggle Plain Text)
  1. output = ""
  2. for i in range(6):
  3. output += str(i)
  4. print output
  5. # prints: 012345
Or you could use a list comprehension:
python Syntax (Toggle Plain Text)
  1. print "".join(str(i) for i in range(6))
  2. # prints 012345
Or you could use sys.stdout.write():
python Syntax (Toggle Plain Text)
  1. import sys
  2. for i in range(6):
  3. sys.stdout.write(str(i))
  4. sys.stdout.write("\n")
  5. # prints 012345
Arevos is offline   Reply With Quote
Old Sep 27th, 2006, 1:56 PM   #4
MR.T
Newbie
 
MR.T's Avatar
 
Join Date: Feb 2006
Location: USA,Michigan
Posts: 22
Rep Power: 0 MR.T is on a distinguished road
Ok, thanks for the help. I fixed my problem using the second method Arevos sugested.
__________________
"Gee, Brain, what do you want to do tonight?" "The same thing we do every night, Pinky: Try to take over the world!"
MR.T is offline   Reply With Quote
Old Sep 27th, 2006, 2:39 PM   #5
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
In the future posting the code you're having trouble with will help you get more helpful posts much quicker. Glad you got it sorted out, though.
__________________
Amateurs built the ark
Professionals built the Titanic

peace_of_mind is offline   Reply With Quote
Old Sep 28th, 2006, 5:46 PM   #6
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Just to be correct, our esteemed Arevos' list comprehension is really a generator expression.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Oct 9th, 2006, 5:01 PM   #7
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
... and generator expressions are new in python2.4
luckily, python2.4 will enter debian stable soon (if etch is on time), which removes my last excuse to stick with python2.3
free-zombie is offline   Reply With Quote
Old Oct 9th, 2006, 6:00 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Are you kidding? The current version of Python is 2.5, you know.
titaniumdecoy is offline   Reply With Quote
Old Oct 11th, 2006, 3:16 PM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Quote:
Originally Posted by titaniumdecoy View Post
Are you kidding? The current version of Python is 2.5, you know.
Unless your customer uses Python 2.3
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Oct 23rd, 2006, 12:30 PM   #10
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Dietrich View Post
Unless your customer uses Python 2.3
Heh! I've been pushing to get rid of 2.2 support from a certain project for a while -- the idioms 2.3 adds are so much nicer. Ah well... 2.5 will go stable sometime... as will 2.6... as will 2.7...

yrs obsolescent-compatibility-hurts-my-eyes-ly, -T.
hydroxide is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Python] BlackJack UnKnown X Show Off Your Open Source Projects 9 Feb 20th, 2006 6:01 AM
First Python Programme: Fibonacci Finder UnKnown X Python 2 Dec 15th, 2005 6:19 PM
2 new programs HaCkeR C 30 Nov 29th, 2005 7:14 AM
2 problems with a script glevine Perl 5 Jul 20th, 2005 12:57 PM
ftplib help please! disAbled Python 1 Jun 30th, 2005 8:05 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC