The print() function prints the given object to the standard output device (screen) or to the text stream file. Syntax of print()

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

where

  • objects – object to the printed. * indicates that there may be more than one object
  • sep – objects are separated by sep. Default value: ‘ ‘
  • end – end is printed at last
  • file – must be an object with write(string) method. If omitted it, sys.stdout will be used which prints objects on the screen.
  • flush – If True, the stream is forcibly flushed. Default value: False

 

Example

a = 5
b = a

# Two objects are passed
print("a =", a)

# Three objects are passed
print('a =', a, '= b')

# Separator and end parameters
print("a =", a, sep='00000', end='\n\n\n')
print("a =", a, sep='0', end='')

# With file parameter
sourceFile = open('print.txt', 'w')
print('Pretty cool', file = sourceFile)
sourceFile.close()

# Output
a = 5
a = 5 = b
a =000005


a =05

# Casting integer to string
print('My age is: ' + str(42))

# Newline character
print('line1\nline2\nline3')

# Using f-sting
print(f'Hello, {4}')