Next: The return statement Up: Simple statements Previous: The del statement

The print statement


print_stmt:     "print" [ condition ("," condition)* [","] ]

print evaluates each condition in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written. A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case: (1) when no characters have yet been written to standard output; or (2) when the last character written to standard output is \n; or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.)

A "\n" character is written at the end, unless the print statement ends with a comma. This is the only action if the statement contains just the keyword print.

Standard output is defined as the file object named stdout in the built-in module sys. If no such object exists, or if it is not a writable file, a RuntimeError exception is raised. (The original implementation attempts to write to the system's original standard output instead, but this is not safe, and should be fixed.)


guido@cwi.nl