In python 3 the function print can get many arguments.
the full signature of the function print is:
print(args*, sep=' ', end='\n', file=sys.stdout, flush=False)
when sep
is the separator of the arguments from args*
, end
is how to end the printed line ('\n\ means a new line) file is to where print the output (stdout is the consul) and flush is if to clean the buffer.
Usage Example
import sys
a = 'A'
b = 0
c = [1, 2, 3]
print(a, b, c, 4, sep=' * ', end='\n' + ('-' * 21), file=sys.stdout, flush=True)
Output
A * 0 * [1, 2, 3] * 4
---------------------
In python there are many ways to format string and even a built in formatted string type.
How to format string
- the
format()
function. (some examples)
- Formatted String Literals or in the common name f-strings.
- format using % (more about this)
Examples
name = 'my_name'
>>> print('my name is: {}'.format(name))
my name is: my_name
# or
>>> print('my name is: {user_name}'.format(user_name=name))
my name is: my_name
# or
>>> print('my name is: {0}'.format(name))
my name is: my_name
# or using f-strings
>>> print(f'my name is: {name}')
my name is: my_name
# or formatting with %
>>> print('my name is: %s' % name)
my name is: my_name