Tutorial - Python 3 basics Print function

This tutorial is for beginners to learn using Python print statement, a great tool to find, remove and fix errors made within code.

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 have to deliver its result in some way. One form of output goes to the standard output by using the print statement in Python. The 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 is a great tool to find, remove and fix errors made within code.

Using the 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 the output.
  • end - end is printed at last. By default '\n' (newline character is used). Notice that each print statement displays the output in the new line.
  • file - must be an object with write(string) method. If omitted, sys.stdout will be used which prints objects on the screen.
  • flush - If True, the stream is forcibly flushed. Default value: False.

Example of how the Print() function works:

print('use print with single quotes')

Or use:

print("or use print with double quotes")

Example how to use Print() with separator and end parameters:

a = 1
print("a =", a, sep='_', end='\n\n')
print("a =", a, sep='_0_', end='')

The 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 the 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')

The 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 the code will try to open the 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 the 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, the file is closed using the 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 enough for basics to move forward.