print ([object, ...] [, sep=' '] [, end='\n'] [, file=sys.stdout] [, flush=False])
- object: list of parameters for output
- sep: separator symbol between items in object list
- end: string defining the way the output pointer is placed after executing the print function
- file: the output device (by default: the screen; could be some file)
- flush: boolean value defining that output values should be flushed immediately to output device without buffering (when set to 'True'; the default is: 'False').
# Examples of using print()
x = 'spam'
y = 9
z = ['eggs']
print(x, y, z)
# no separator
print(x, y, z, sep='')
# comma and space as separator
print(x, y, z, sep=', ')
# '#' as separator
print(x, y, z, sep='#')
# no change of line for the next print that uses '-' separator
print(x, y, z, end='')
print(x, y, z, sep='-')
- a) escapes special characters in a string
- b) breaks a logical line into more physical lines
Certain strings introduced by backslash special character have a special meaning:
- '\n' moves one line down (= LF, Line Feed)
- '\r' moves to the beginning of line (= CR, Carriage Return)
- '\t' moves to next tab (= 8 spaces)
See a table of Python escape sequences here (scroll down the page a bit)
# Displays x, y and moves to next line to display z
print(x, y, '\n', z)
# Displays in tabbed positions
print(x, y, z, sep='\t')
# The separator moves to beginning of line after each variable is printed; so, finally only the value of z appears
print(x, y, z, sep='\r')
print('Here backslash \'escapes\' single quotes')
print('No need for "backslash" here')
print('Here backslash escapes \\ backslash \\')
asum = 1+2+3+4+5+6+7+8+9+10+\
11+12+13+14+15+16+17+18+19+20
print(asum)
print(1+2+3+4+5+6+7+8+9+10+
11+12+13+14+15+16+17+18+19+20)
print('This is a long long long long long long long long long message')
print('This is a long long long long long\
long long long long message too')
For efficient printing of multiple values, use the .format() method work as follows:
STEP-1
- Write a print() function with a string that contains as many {:} placeholders as the variables you want to display
- Then add the .format() string at the end. Your print() appears now like this:
print('{:}{:}{:}'.format())
STEP-2
- Add in placeholders description of the display space that is required.
- Add in the format() parentheses the list of parameters to display. Now your print() looks like:
print('{:5d} {:6.3f} {:10d}'.format(a,b,z))
- For example {:5d} means preserve a space of 5 digits to present the value.
STEP-3 (optional)
- Finally, you may define (if you wish) the order of appearence of the values. This can be done by adding numerical values representing the order in front of the ':' sign in placeholders
print('{:5d} {:6.3f} {:10}'.format(a,b,z))
- For example {2:5d} means present the value in the third place of .format() (remember Python is zero-ordered!)
a = 16
b = 2.25
z = 45
print('{:5d} {:6.3f} {:10d}'.format(a,b,z))
print('{2:5d} {1:6.3f} {0:10d}'.format(a,b,z))
a = 'Uno'
b = 'Dos'
z = 'Tres'
print('{2:5} {1:5} {0:5}'.format(a,b,z))
. Free learning material
. See full copyright and disclaimer notice