Printing to stdout in Python: Difference between revisions
Jump to navigation
Jump to search
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | |||
=Internal= | =Internal= | ||
* [[Python_Language#Overview|Python Language]] | * [[Python_Language#Overview|Python Language]] | ||
* [[Python_Code_Examples#Code_Examples|Python Code Examples]] | * [[Python_Code_Examples#Code_Examples|Python Code Examples]] | ||
=Overview= | =Overview= | ||
To send a string followed by new line to <code>stdout</code>: | To send a string followed by new line to <code>stdout</code>: | ||
Line 15: | Line 15: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<code>print()</code> strips [[Python_Language_String#Quotes|quotes]] from strings, resolves escaped characters and displays the resulting content: | <code>print()</code> strips [[Python_Language_String#Quotes|quotes]] from strings, resolves [[Python_Language_String#Escaped_Characters|escaped characters]] and displays the resulting content: | ||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
print('this is a\nmulti-line string') | print('this is a\nmulti-line string') | ||
Line 23: | Line 23: | ||
this is a | this is a | ||
multi-line string | multi-line string | ||
</syntaxhighlight> | |||
<code>print()</code> add spaces between its arguments and a newline at the end: | |||
<syntaxhighlight lang='py'> | |||
print('A', 'B', 'C') | |||
</syntaxhighlight> | |||
results in: | |||
<syntaxhighlight lang='text'> | |||
A B C | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 29: | Line 38: | ||
print('something', file=sys.stderr) | print('something', file=sys.stderr) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=<tt>pprint()</tt>= | |||
<font color=darkkhaki>TODO [[IPy]] Print Nicely with pprint() Page 122.</font> |
Latest revision as of 01:47, 20 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)
pprint()
TODO IPy Print Nicely with pprint() Page 122.