Print in PYTHON

ROHITH SAI REDDY
4 min readMay 15, 2021

--

We all know how basic is print function in python, but do we know its complete capabilities?

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

This is the complete syntax of print function as per the python documentation given.

Let us go through all the parameters that the function takes.

  1. Objects: This objects are the ones that will be printed to the output.
  2. Sep: This allows the user to separate the objects with the value given in the sep attribute.
  3. End: Any value given in the place of end in the print function prints it at the end of the each line, just once at the end if there is only one line to print.
  4. File: This should be the object including the write(string) method, if not mentioned the default value will be sys.stdout. Which will print the objects to the terminal.
  5. Flush: This forcibly flushes the stream, if this attribute is set the function will not buffer the data to increase the efficiency and will keep flushing it on each call of the function .Default value is set to False.

Use of * in print function

This denotes a sequence to print in python. This is generally denotes right at the starting of the print method to denote that it is a sequence to be printed. Let us now see an example how * is used.

This is the basic print statement without * operator, so it will just print it as a list.

Now when we include the * operator.

It prints like a sequence of items rather than just printing it as a list.

Let us see in case of dictionaries. Using Counter to make a dictionary.

Normal print function would give a dictionary with all the frequencies as output but when we include the * it gives out the keys of the dictionary alone.

Without using *
Using *

So, we can say that using the * will give output as a sequence.

Printing to File

To print to file we will be using file parameter of print function.

In this fashion we can print the output to the file.

pprint

This is a module in python which can be used to print objects in the formatted structures. This comes so handy while printing the objects that are not python literals such as files, class objects, data from sockets, etc.

To use this we need to import the module pprint.

Syntax of calling this function is:

pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True)

Here we can also set the format in which we want to print using the syntax

pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True)

This gives us so much readability when we are getting data in an unorganised fashion as like from any URL source.

For example, let us get data from a url and print it using pprint and normal print and see the difference. After importing the modules json, pprint, urllib.request.

This is normal print function.

This gives such a hard time to read and get insights of the data gathered.

Using pprint to print the data

From the above example we can see from the datatype that is not defined in python, we could arrange and print it in a readable form using the pprint module.

Conclusion

Above are few illustrated ways to effectively use the print function in python.

Thank you.

--

--

No responses yet