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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-14-2010, 04:22 PM
|
#1
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
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?
|
|
|
|
06-14-2010, 04:40 PM
|
#2
|
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
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.
|
|
|
|
06-14-2010, 05:51 PM
|
#3
|
|
Member
Registered: Oct 2009
Posts: 451
Rep: 
|
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.
|
|
|
|
06-14-2010, 07:25 PM
|
#4
|
|
Senior Member
Registered: May 2005
Posts: 4,386
|
Quote:
Originally Posted by MTK358
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
|
|
|
|
06-14-2010, 08:02 PM
|
#5
|
|
Senior Member
Registered: May 2005
Posts: 4,386
|
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 ?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
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
|
|
|
All times are GMT -5. The time now is 11:27 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|