The print() function is used to output text to an output stream such as the console, a file, a GUI window, etc.

ExampleEdit & Run
country = 'Japan'
capital = 'Tokyo'

#print to the console
print('country: ', country)
print('capital: ', capital)
Output:
country:  Japancapital:  Tokyo[Finished in 0.010741796111688018s]

The syntax for using the print function is as follows:

Syntax:
print(*values, sep=' ', end='\n', file=sys.stdout, flush=False).
*values The arbitrary objects to be printed, the objects can be of any valid types such as string, integers, lists, user defined objects, etc. If an object is not a string, it will be converted to its string representation before it is printed.
sep An optional string specifying the value to separate the printed items(if more than one). If not given, it defaults to a space(" "
end An optional string specifying the value to be printed at the end after all the values have been printed. It defaults to a new line character("\n")
file An object with a write() method where the print values will be written. If not given, it defaults to sys.stdout which is literally the console.
flush A Boolean, specifying if the output is flushed (True) or buffered (False). It defaults to False.
ExampleEdit & Run
print('Python')
print([1, 2, 3])
print(1, 'Python', [1, 2, 3, 4])
Output:
Python[1, 2, 3]1 Python [1, 2, 3, 4][Finished in 0.009999736910685897s]

The sep argument

The optional sep argument specifies the separator that will be inserted between objects when multiple objects are passed to the print function,  it defaults to a space.

ExampleEdit & Run
print('Python', 'Java', 'PHP', 'C++')

print('Python', 'Java', 'PHP', 'C++', sep = ', ')

print('Python', 'Java', 'PHP', 'C++', sep = ' - ')
Output:
Python Java PHP C++Python, Java, PHP, C++Python - Java - PHP - C++[Finished in 0.010264581069350243s]

The end argument

The optional end argument specifies the characters to be printed after all items have been printed, the default is a new line character, '\n'.

ExampleEdit & Run
print( 'Python', 'C++', 'Java', 'PHP', end = ' ' )

print( 'Python', 'C++', 'Java', 'PHP', end = ',' )
Output:
Python C++ Java PHP Python C++ Java PHP,[Finished in 0.009874840965494514s]

The flush argument

The flush parameter is a Boolean parameter that indicates whether the buffered output should be flushed or not.  By default,  the output data is buffered meaning that it is written in chunks.

When flush is set to True, the buffered data is immediately written to the output stream. Setting flush to True can also help reduce memory usage if data must be written at regular intervals.

ExampleEdit & Run
print('Python','Java', 'C++', 'PHP', flush = True)
Output:
Python Java C++ PHP[Finished in 0.009788847994059324s]

Writing to a custom file

The file argument is also optional and specifies an output device such as a file,  the default is the system's standard output i,.e the console\terminal.

ExampleEdit & Run
with open('my_file.txt', 'w') as file:
     
     print( 'Hello, Word', file = file )

in the above program, the string  "Hello, world" will be printed  on the file named my_file.txt.

Writing to a custom objects.

We can make a custom object support being written on by defining the write() method. We then specify the file argument to be the custom object so that the print() function will output the values to the object.

The write() method should take the data to be written and apply it to the object. 

ExampleEdit & Run
class myFile: 
    def __init__(self): 
         self.data = []

    #define the write method
    def write(self, value): 
        self.data.append(value)

f = myFile()
print('Hello', file = f)
print('World!', file = f)
print('Welcome!', file = f)
print([1, 2, 3, 4], file = f)

# get the values
print(f.data)
Output:
['Hello', '\n', 'World!', '\n', 'Welcome!', '\n', '[1, 2, 3, 4]', '\n'][Finished in 0.009420279879122972s]