[Tutor] Printing to Screen
Jeff Shannon
jeff@ccvcorp.com
Wed Jul 30 16:50:03 2003
DORSEY_EDMUND_K@LILLY.COM wrote:
>
> Is there a way to print but not have it do a new line
>
> For instance in java you have print and println in C you have to
> specify the end of line marker.
>
> Whenever I use print in python is puts a newline.
If you end your print statement with a comma, then you'll (probably) be
given a space instead of a newline -- I say probably because if the line
already ends in whitespace, print will not add more. This feature is
called "softspace", and it's intended to help print be a quick & easy
way to display simple things -- but print is not designed to give you
complete control over your output.
If you need finer control over the output than print gives you, then you
can import sys and use sys.stdout.write() -- sys.stdout is a file-like
object that represents your current standard output, and print
internally writes to sys.stdout. You can use most file methods on
sys.stdout (though not seek-related ones). Keep in mind that, because
sys.stdout is a buffered I/O device, you may need to call
sys.stdout.flush() to actually have written data appear on the screen.
Jeff Shannon
Technician/Programmer
Credit International