Multiple ways to format and print in python

In this article, we shall see how to print data to the console using python. We all know that the print() method can be used to print data to the console. This article mainly focuses on the multiple ways by which can format the data before printing it out on the console.

The following methods can be used to format and print data or value to the console.

  1. Printing without any formatting
  2. Using % to format
  3. Passing the values as a tuple
  4. Passing the values as a dictionary
  5. Using the format() method
  6. Format method with indexing
  7. Format method with key-value pair
  8. String concatenation
  9. Passing the values as parameters
  10. f-string formatting

Let us look at all these methods one by one with an example for each.

Printing without any formatting

The first method is printing a string directly without formatting it. We all know how to do this. I just added this as a warm-up to all the other methods that are about to come.

Using % to format

From this method onwards we will be focusing on the methods to modify the string before printing it to the terminal or console. The % operator can help you format the string like this.

Passing the values as tuple

We can also pass the values as a tuple and then format it using the % operator. Let us see it with an example. This is similar to the above approach.

Passing values as a dictionary

We can also pass the values as a dictionary. If we pass a dictionary for formatting we have to modify the %s to be something like this “%(key)s”.

Using the format() method

Python provides a format() method to format the strings. This is easier and much simpler than using %s to format the strings. The strings will have curly braces indicating that those places have to be formatted.

The format method will have the values separated by a comma. By default, the first curly brace will be formatted by the first value in the format method, second curly brace with the second value, and so on.

format method with indexing

The format method also supports indexing. This will be helpful if you want to rearrange the values while printing. The index value should be specified inside the curly braces.

This overrides the default behaviour of filling the first curly brace with the first value, second curly brace with the second value and so on.

format method with key-value pair

The format method will also accept a key-value pair to format the string before printing. If you feel like indexing a bit confusing you can choose this method.

The key has to be given inside the curly braces.

string concatenation

We can also concatenate the strings before printing to the terminal. This is not a very good approach.

Passing the values as parameters

This is similar to string concatenation. Provide the values in the appropriate values. The system by default will add spaces.

The f-string formatting

This one is the best in my opinion. However this is supported only in python 3.6+

Conclusion

Hope this article is helpful. If you have any queries or if I have missed any other methods, leave them below in the comments.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *