Printing to stdout in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 25: Line 25:
</syntaxhighlight>
</syntaxhighlight>


<code>print()</code> add spaces between its arguments:
<code>print()</code> add spaces between its arguments and a newline at the end:
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
print('A', 'B', 'C')
print('A', 'B', 'C')

Revision as of 20:04, 18 June 2022

External

Internal

Overview

To send a string followed by new line to stdout:

print('something')

To avoid sending a new line at the end:

print('something', end='.')
print('something', end='')

print() strips quotes from strings, resolves escaped characters and displays the resulting content:

print('this is a\nmulti-line string')

results in:

this is a
multi-line string

print() add spaces between its arguments and a newline at the end:

print('A', 'B', 'C')

results in:

A B C

Redirect to stderr

print('something', file=sys.stderr)