| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="return.html" /> |
| 13 | <link rel="prev" href="del.html" /> |
| 14 | <link rel="parent" href="simple.html" /> |
| 15 | <link rel="next" href="return.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.6 The print statement </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="6.5 The del statement" |
| 25 | href="del.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="6. Simple statements" |
| 28 | href="simple.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="6.7 The return statement" |
| 31 | href="return.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="del.html">6.5 The del statement</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="simple.html">6. Simple statements</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="return.html">6.7 The return statement</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION008600000000000000000"></A><A NAME="print"></A> |
| 56 | <BR> |
| 57 | 6.6 The <tt class="keyword">print</tt> statement |
| 58 | </H1> |
| 59 | <a id='l2h-495' xml:id='l2h-495'></a> |
| 60 | <P> |
| 61 | <dl><dd class="grammar"> |
| 62 | <div class="productions"> |
| 63 | <table> |
| 64 | <tr> |
| 65 | <td><a id='tok-print_stmt' xml:id='tok-print_stmt'>print_stmt</a></td> |
| 66 | <td>::=</td> |
| 67 | <td>"print" ( <big>[</big><a class='grammartoken' href="Booleans.html#tok-expression">expression</a> ("," <a class='grammartoken' href="Booleans.html#tok-expression">expression</a>)* <big>[</big>","<big>]</big><big>]</big></td></tr> |
| 68 | <tr> |
| 69 | <td></td> |
| 70 | <td></td> |
| 71 | <td><code>| "><code>></code>" <a class='grammartoken' href="Booleans.html#tok-expression">expression</a> |
| 72 | <big>[</big>("," <a class='grammartoken' href="Booleans.html#tok-expression">expression</a>)+ <big>[</big>","<big>]</big><big>]</big> )</code></td></tr> |
| 73 | </table> |
| 74 | </div> |
| 75 | <a class="grammar-footer" |
| 76 | href="grammar.txt" type="text/plain" |
| 77 | >Download entire grammar as text.</a> |
| 78 | </dd></dl> |
| 79 | |
| 80 | <P> |
| 81 | <tt class="keyword">print</tt> evaluates each expression in turn and writes the |
| 82 | resulting object to standard output (see below). If an object is not |
| 83 | a string, it is first converted to a string using the rules for string |
| 84 | conversions. The (resulting or original) string is then written. A |
| 85 | space is written before each object is (converted and) written, unless |
| 86 | the output system believes it is positioned at the beginning of a |
| 87 | line. This is the case (1) when no characters have yet been written |
| 88 | to standard output, (2) when the last character written to standard |
| 89 | output is "<tt class="character">\n</tt>", or (3) when the last write operation on |
| 90 | standard output was not a <tt class="keyword">print</tt> statement. (In some cases |
| 91 | it may be functional to write an empty string to standard output for |
| 92 | this reason.) <span class="note"><b class="label">Note:</b> |
| 93 | Objects which act like file objects but which are |
| 94 | not the built-in file objects often do not properly emulate this |
| 95 | aspect of the file object's behavior, so it is best not to rely on |
| 96 | this.</span> |
| 97 | <a id='l2h-503' xml:id='l2h-503'></a> |
| 98 | <a id='l2h-496' xml:id='l2h-496'></a> |
| 99 | <P> |
| 100 | A "<tt class="character">\n</tt>" character is written at the end, unless the |
| 101 | <tt class="keyword">print</tt> statement ends with a comma. This is the only action |
| 102 | if the statement contains just the keyword <tt class="keyword">print</tt>. |
| 103 | <a id='l2h-497' xml:id='l2h-497'></a><a id='l2h-498' xml:id='l2h-498'></a> |
| 104 | <P> |
| 105 | Standard output is defined as the file object named <code>stdout</code> |
| 106 | in the built-in module <tt class="module">sys</tt>. If no such object exists, or if |
| 107 | it does not have a <tt class="method">write()</tt> method, a <tt class="exception">RuntimeError</tt> |
| 108 | exception is raised. |
| 109 | <a id='l2h-499' xml:id='l2h-499'></a><a id='l2h-504' xml:id='l2h-504'></a> |
| 110 | <a id='l2h-501' xml:id='l2h-501'></a><a id='l2h-502' xml:id='l2h-502'></a> |
| 111 | <P> |
| 112 | <tt class="keyword">print</tt> also has an extended<a id='l2h-505' xml:id='l2h-505'></a> |
| 113 | form, defined by the second portion of the syntax described above. |
| 114 | This form is sometimes referred to as ``<tt class="keyword">print</tt> chevron.'' |
| 115 | In this form, the first expression after the <code>></code><code>></code> must |
| 116 | evaluate to a ``file-like'' object, specifically an object that has a |
| 117 | <tt class="method">write()</tt> method as described above. With this extended form, |
| 118 | the subsequent expressions are printed to this file object. If the |
| 119 | first expression evaluates to <code>None</code>, then <code>sys.stdout</code> is |
| 120 | used as the file for output. |
| 121 | |
| 122 | <P> |
| 123 | |
| 124 | <DIV CLASS="navigation"> |
| 125 | <div class='online-navigation'> |
| 126 | <p></p><hr /> |
| 127 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 128 | <tr> |
| 129 | <td class='online-navigation'><a rel="prev" title="6.5 The del statement" |
| 130 | href="del.html"><img src='../icons/previous.png' |
| 131 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 132 | <td class='online-navigation'><a rel="parent" title="6. Simple statements" |
| 133 | href="simple.html"><img src='../icons/up.png' |
| 134 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 135 | <td class='online-navigation'><a rel="next" title="6.7 The return statement" |
| 136 | href="return.html"><img src='../icons/next.png' |
| 137 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 138 | <td align="center" width="100%">Python Reference Manual</td> |
| 139 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 140 | href="contents.html"><img src='../icons/contents.png' |
| 141 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 142 | <td class='online-navigation'><img src='../icons/blank.png' |
| 143 | border='0' height='32' alt='' width='32' /></td> |
| 144 | <td class='online-navigation'><a rel="index" title="Index" |
| 145 | href="genindex.html"><img src='../icons/index.png' |
| 146 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 147 | </tr></table> |
| 148 | <div class='online-navigation'> |
| 149 | <b class="navlabel">Previous:</b> |
| 150 | <a class="sectref" rel="prev" href="del.html">6.5 The del statement</A> |
| 151 | <b class="navlabel">Up:</b> |
| 152 | <a class="sectref" rel="parent" href="simple.html">6. Simple statements</A> |
| 153 | <b class="navlabel">Next:</b> |
| 154 | <a class="sectref" rel="next" href="return.html">6.7 The return statement</A> |
| 155 | </div> |
| 156 | </div> |
| 157 | <hr /> |
| 158 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 159 | </DIV> |
| 160 | <!--End of Navigation Panel--> |
| 161 | <ADDRESS> |
| 162 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 163 | </ADDRESS> |
| 164 | </BODY> |
| 165 | </HTML> |