Tutorial - Python 3 basics
Print function

Posted September 25, 2018 by Rokas Balsys

There are hardly any computer programs and of course, hardly any Python programs, which don't communicate with the outside world. On a computer all programs has to deliver its result in some way. One form of output goes to the standard output by using the print statement in Python. Print function in Python is a function that outputs to your console window whatever you want to print out. It is actually one of the most widely used functions in all of Python, the same as in other languages. The main reason to use is that it is a great debugging tool. In other words talking, it is a great tool to find, remove and fix errors made within code.

Using Python print() function it prints the given object to the standard output device (screen) or to our text file. Original Syntax of print() function is:

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

###### Print() function parameters:
• objects - object to be printed. * indicates that there may be more than one object.
• sep - objects are separated by sep. Default value: ' '. Notice, the space between two objects in output.
• end - end is printed at last. By default '\n' (newline character is used). Notice that each print statemens displays the output in the new line.
• 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 how Print() function works:
print('use print with single quotes')


Or use:

print("or use print with double quotes")


###### Example how to use Print() with seperator and end parameters:
a = 1
print("a =", a, sep='_', end='\n\n')
print("a =", a, sep='_0_', end='')


Result will be:

a =_1

a =_0_1
>>>

Keep in mind that you used "\n\n" - this means you got one additional blank line.

###### More about print with single and double quotes:

Sometimes we want to write for example word "can't", but we can't do it like this:

print('Can't write like this')


In this case, we need to use double quotes for main string:

print("Can't write like this")


Or we can use the "\" backslash, which is known as an "escape" character:

print('You\'ll be able to write sentences like this')


Result will be:

You'll be able to write sentences like this


###### Example how to use Print() with file parameters:
sourceFile = open('python.txt', 'w')
print('We are printing something to txt file', file = sourceFile)
sourceFile.close()


This part of code will try to open python.txt file in writing mode. If this file doesn't exist, it will be created and opened in writing mode.
Here, we have passed source file object to the 'file' parameter. String: 'We are printing something to txt file' should now be printed to python.txt file. After that, file is closed using close() method. (Check it in your system if you tried this tutorial on your own).

Now we covered the basics of strings for now. There is much more about strings, but it's enought for basics to move forward. Now you can move to next tutorial: