Difference between revisions of "Print function"

m
Line 7: Line 7:
  
 
By way of example, the following are some sample print statements, and their output
 
By way of example, the following are some sample print statements, and their output
  print(5)
+
  >>> print(5)
    5
+
5
  print('this is a test')
+
  >>> print('this is a test')
    this is a test
+
this is a test
  print('this','is','another','test') # note that spaces are inserted
+
  >>> print('this','is','another','test') # note that spaces are inserted
    this is another test
+
this is another test
  print('this'+'is'+'another'+'test') # no spaces are automatically inserted, because there is only one argument
+
  >>> print('this'+'is'+'another'+'test') # no spaces are automatically inserted, because there is only one argument
    thisisanothertest
+
thisisanothertest
  print('The Answer is', 42)
+
  >>> print('The Answer is', 42)
    The Answer is 42
+
The Answer is 42
  
 
==See Also==
 
==See Also==

Revision as of 23:22, 10 March 2011

In Python, the print function prints things to the screen. In its simplest form it has the following syntax

print(arg1[,arg2[,...]])

More than one argument can be passed to print at a time. By default, multiple arguments are printed on the same line with a space inserted between each one and at the end of each print statement Python will insert a new line, so subsequent calls to print will output on consecutive lines. These default behaviors can be overridden, see the Python 3.2 Documentation for details.

If the arguments aren't already strings, print will convert them to strings just as str would.

By way of example, the following are some sample print statements, and their output

>>> print(5)
5
>>> print('this is a test')
this is a test
>>> print('this','is','another','test') # note that spaces are inserted
this is another test
>>> print('this'+'is'+'another'+'test') # no spaces are automatically inserted, because there is only one argument
thisisanothertest
>>> print('The Answer is', 42)
The Answer is 42

See Also

Python 3.2 Documentation