The print() function normally displays a line of output.
print('Apple')
print('Banana')
print('Cherry')
Suppressing the print() function's ending newline.
print('Apple', end=' ')
print('Banana', end=' ')
print('Cherry')
When multiple arguments are passed, they are separated by spaces by default:
print('Apple', 'Banana', 'Cherry')
To change the space separator, pass the sep argument:
print('Apple', 'Banana', 'Cherry', sep='*')
#remove the space:
print('Apple', 'Banana', 'Cherry', sep='')
An escape character is a special character that is preceded with a backslash '\', appearing inside a string literal. Full list with examples: Python Escape Characters
#newline
print ('One\nTwo\nThree')
#a tab
print ('\tPrinting a tab')
#a backslash
print ('\\Printing a backslash')
num = 10000 / 3
num
Precision of two decimal places:
format(num, '.2f')
Comma separator and precision of two decimal places:
format(num, ',.2f')
A minimum field width is the minimum number of spaces that should be used to display the value.
# min field width of 5 characters and 0 precision
format(num, '5.0f')
# same as above, add coma separator
format(num, '5,.0f')
Formatting a floating-point number as a percentage.
format (.5, '%')
#0 precision
format (.5, '.0%')
Integer has no precision.
format (123456, 'd')
With a comma separator:
format(123456, ',d')
In a field that is 8 spaces wide:
format(123456, '8,d')