LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-14-2010, 05:22 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Python output


I have two questions about Python's print statement:

1) How to make it not stick spaces and newlines between items in its provided list?

2) How to print to stderr?
 
Old 06-14-2010, 05:40 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Your first question is a bit ambiguous, provide an example of what you mean, in answer to your second question:

Code:
#!/usr/bin/python
import sys
print >> sys.stderr, 'this goes to stderr'
print >> sys.stdout, 'this goes to stdout'
Code:
core:~/vir$ ./test.py
this goes to stderr
this goes to stdout
core:~/vir$ ./test.py 2>std.err
this goes to stdout
core:~/vir$ ./ test.py 1>std.out
this goes to stderr
core:~/vir$
Edit: on second thought--

Code:
#!/usr/bin/python
import sys
print 'this', 'is', 'a', 'list', 'of', 'items.'
sys.stdout.write("%s%s%s\n" % ("3.14", "pi.", " mmmmm pie."))
Code:
core:~/vir$ test2.py
this is a list of items.
3.14pi. mmmmm pie.
core:~/vir$
I think is what you were looking for.

Last edited by rweaver; 06-14-2010 at 05:47 PM.
 
Old 06-14-2010, 06:51 PM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
As for newlines in a regular print - to not stick a newline at the end, stick a comma at the end:

Code:
>>> print 'no newline between here',
... print 'and here'
no newline between here and here
Don't think there's a 'quick' way for spaces, you have to do what rweaver suggested.
 
Old 06-14-2010, 08:25 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by MTK358 View Post
I have two questions about Python's print statement:

1) How to make it not stick spaces and newlines between items in its provided list?

2) How to print to stderr?
Just wondering - entering

Python print stderr

into yahoo.com as the third match I see:

http://diveintopython.org/scripts_an...ut_stderr.html
.

And there is an example:

Code:
Example 10.11. Printing to stderr

>>> print 'entering function'
entering function
>>> import sys
>>> print >> sys.stderr, 'entering function' 1
entering function
.

So, do yo perform WEB search and can you use 'Find' function of your favorite WEB browser ?




So, did you at all try WEB search ? Do
 
Old 06-14-2010, 09:02 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
And, MTK358, entering into Yahoo

Python print newline

as the second match I'm getting:

http://codingrecipes.com/print-witho...pace-in-python

- do you see "print-without-a-new-line-or-space-in-python" in the URL ?

And you know, indeed there is an example:

Code:
Now, you can fix the issue with the line break by inserting a comma after the print statement:

print "Hello",
print "Python",
print "!"

This will print:
Hello Python !

But the problem is that it will still insert a space after it prints the text, so to fix this you will have to use sys.stdout.write like this:

import sys
sys.stdout.write("Hello ")
sys.stdout.write("Python")
sys.stdout.write("!")

This will print:
Hello Python!
So, do yo perform WEB search and can you use 'Find' function of your favorite WEB browser ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Colored output in Python MTK358 Programming 2 12-24-2009 09:01 PM
Python serial output seems strange edM Programming 2 04-27-2009 05:54 AM
python shell doesn't run idle output me_lax Linux - Newbie 3 09-14-2008 12:55 AM
How do you output PCM sounds linux using python and ALSA? jjdb6185 Programming 1 07-13-2005 03:39 PM
python read output from seperate script? bendeco13 Programming 1 02-01-2005 11:38 PM


All times are GMT -5. The time now is 09:11 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration