LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 04:22 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 04:40 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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 04:47 PM.
 
Old 06-14-2010, 05:51 PM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
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, 07:25 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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, 08:02 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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



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 08:01 PM
Python serial output seems strange edM Programming 2 04-27-2009 04:54 AM
python shell doesn't run idle output me_lax Linux - Newbie 3 09-13-2008 11:55 PM
How do you output PCM sounds linux using python and ALSA? jjdb6185 Programming 1 07-13-2005 02:39 PM
python read output from seperate script? bendeco13 Programming 1 02-01-2005 10:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration