The python print function and Various Ways to Use it in Your Code

When you are just introduced to a programming language, the very first line of code that you use is to display the words using the python print feature:

“Hello World!”

There is no need to worry about Python since its syntax is very straightforward. It highly encourages its programmers to program their codes without the prepared code. The python print is the simplest one you will find in Python since it is a function that prints a line. It also has the ability print a newline, which you can’t do with the C language.

As of this writing, there are 2 major versions of Python – Python 3 and Python 2. They are quite different from each other, though. The syntax here is from Python 3, so you better secure a Python 3 version and start running the following script and save it as hello world.py.

print(“Hello World!”)

The result to this after running the script is as follows:

Hello World!

Full syntax of the python print function

The line of code above is very simple. However, if you are to understand the full syntax of the print() function, it is as follows:

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

Below are the following parameters of the print() function:

  • objects – this is the object that will be printed by the function. The asterisk symbol defines that more than one object can be utilized by the function itself.
  • sep – the objects that are separated by the sep.
  • end
  • file – this should be an object that has either a string or write method. If this is omitted, the sys.stdout is used in which it prints the objects onto the screen
  • flush – if this is True, the stream gets flushed forcibly. Its default value is False.

Keep in mind that end, sep, flush and file are all keyword argument. If you want to use the sep argument as an example, it would be to write the following:

print(*objects, sep = ‘separator’)

And not to be done this way:

print(*objects, ‘separator’)

Return value

There is no return value of the print() function. All it returns is None.

 

Various examples of how the python print works

print(“Python is fun.”)

a = 5

# Two objects are passed

print(“a =”, a)

b = a

# Three objects are passed

print(‘a =’, a, ‘= b’)

When the codes above run, below is the output:

Python is fun.

a = 5

a = 5 = b

The only parameter that is passed onto the pyhon print function is only objects. This is why there is the need to use the ‘ ‘ separator. If you take a closer look, you will find a space between the 2 objects in the output. The end parameter, the newline character – ‘\n’ is used. You you will find that every print statement shows the output with a new line. The file parameter here is the sys.stdout, where the output is printed through the screen. The value of flush is its default value, False. Thus the stream is not flushed forcibly.

Another example of using the print() function below makes use of end parameters and a separator:

a = 5

print(“a =”, a, sep=’00000′, end=’\n\n\n’)

print(“a =”, a, sep=’0′, end=”)

The output of this will be:

a =000005  a =05

The example below is using the print() function with the file parameter. If you find this a bit difficult to understand at this point, you should try to understand more about the Python File I/O. You will find the following lines of codes clearer.

sourceFile = open(‘python.txt’, ‘w’)print(‘Pretty cool, huh!’, file = sourceFile)sourceFile.close()

The result of this program opens up the file python.txt while in writing mode. If the file does not exist, the file python.txt is created and then activated in writing mode. In here, the file object sourceFile is passed onto the parameter file. The ‘Pretty cool, huh!’ is the string object, which means that it is printed onto the file python.txt. You will need to check the system if it really made an output. The file also need to be closed, which is why the close() method is being called at the last line of code.

SHARE