Print

print two strings with space between
>>> s1 = 'Hello'; s2 = 'world'
>>> print(s1 + ' ' + s2)
Hello world

better with python3 syntax
>>> from __future__ import print_function
>>> print(s1, s2, sep=' ')
Hello world

print centered '^' within line length of 40 letters (left: '<'  right: '>')
>>> print(format(s1,'^40'))
                      Hello  
>>> print(format(s1,'>40'))
                                   Hello

Print floating-point numbers


# left aligned of 10 digit line length with two digits after point (rounded)
x=65.246
print(format(x,'10.2f'))
65.25

# rounded 2 digits after comma, but without leading spaces
format(x,'.2f') 
65.25

'{:.2f}'.format(x)
'65.25'
'{:06.2f}'.format(x)
'065.25'

Print list elements

strings and numbers
>>> mylist = ['samples', 165]
>>> print(mylist)
['samples', 165]
a) apply the list as separate arguments, print converts each element to a string
>>> print(*mylist)
samples 165
>>> print(*mylist, sep=': ') # specify own separator
samples: 165
b) convert all numbers to strings before joining
>>> print(': '.join(str(l) for l in mylist))
samples: 165





Subpages (1): Error