Essential Python Print Statement Tips and Tricks for Every Programmer – Brain Mentors Skip to content

Essential Python Print Statement Tips and Tricks for Every Programmer

Learn various tips and tricks based on print()

Various Tips and Tricks:

To perform this tutorial step-by-step with me, you’ll need Python3 already configured on your local development machine. You can set up everything you need before-hand and then come back to continue ahead.

1. Printing sum of two numbers

Printing sum of two numbers is a two-step process:

  1. Define two variables, say a and b and store their sum in third variable, say c.
  2. Print out the sum stored in the third variable, i.e., c.

Both these steps can be implemented in numerous ways in various languages. But we will be using the most basic syntax available with Python’s print statement.

  • print(“Sum of”, a, “and”, b, “is”, c)

If you want to use more easier syntaxes for doing the same task or this syntax seems difficult to write when you have loads of variables then you can follow on for more better tricks to use with python( ).

  • C-inspired syntax 😉 : Most of us are already acquainted with print statement of C-language. So, let’s see the same thing in action in Python (we will still need to take care of data types of variables). Here’s the screenshot for this statement:
  • str.format( ) 🧐We can use format( ) available with each String object in Python. It is used to format the string at runtime, i.e., fill in the values just like last syntax did, we just use { } (curly braces) instead of %d or %s. But, the benefit we get is that we don’t need to worry about commas, ‘+’ signs and even data types. str.format( ) allows us to format strings effortlessly.
  • str.format( ) with a twist 🐍In the above syntax, you can use a little variation. Instead of writing variables in a order in which they should replace curly braces, you can write names of variables inside format( ) in any order, and use indexes inside curly braces to select a particular variable whose value should replace that particular curly braces. Keep in mind, the numbering of variables start from because they act like a tuple. This syntax is very helpful when you have got loads of { } to fill up. You can use this variation like this:

We’ll move forward after these 3 syntaxes to look at the latest and easiest print syntax released in Python 3.7. To do that, we need to

  • Use f-strings instead of format( ), which are nothing but an improvement over it. For that, we need to write ‘f’ just before our string begins to indicate Python that we want to use f-strings
  • Use { } containing names of variables, instead of empty curly braces, and omit the format( ) part, which makes the complete statement quite shorter.

Go back to your editor and try this syntax to get started with fast-strings (f-strings):

Let’s talk about the next feature -

  • Writing multiple lines using print( ) : Firstly, we will try to print multi-lines of string (or a basic menu for a simple calculator) using print( ). But, the problem is we will need to use an escape character – newline character (\n) so that we can print out our string in multiple lines.
  • Using multi-line feature of print( ) : Next, we will try to ease our way through writing multiple lines of text (just like the menu that we created in the last step) by using triple quotes (single or double, each of them three times) to activate multi-line feature of print( ). This is a specialized feature with which you can integrate any other syntax that we have used up till here for printing variables. Multi-line statement behaves in a way where it maintains all the whitespaces and newlines entered by the programmer while writing text in print( ).

We will use this feature along with other syntaxes later on to catalyse its the power.

 

  • Walrus Operator : Finally, we have got the latest feature introduced in Python 3.8 – walrus operator ( := ). This operator helps us in calculating values, storing the results, and printing out those results in a single statement. So, we can perform all the three tasks stated above using walrus operator like this:
  • The Super-trick – All Tricks Integrated​ : Now let’s integrate various techniques together like multi-line print statement, walrus operator and the most important one, f-strings. You can typically use this syntax anywhere you want but sometimes when our task is small you can prefer to utilize any trick that satisfies you. However, there is an issue when we use walrus operator and f-strings together – walrus operator has lowest precedence so our string gets formatted even before walrus operator is able to store some result in our variable, and ultimately it results in an error. So, we will enclose our walrus operator part inside parenthesis so that it is calculated first. Use this super-trick like a pro as shown:
  • Bonus – print ( ) + input( ) + walrus : Let’s see another syntax using once again three syntaxes together – print( ) and input( ) combined together using walrus operator. You, generally, greet someone by first asking their name (using input( )) and then greet them by using print( ) and the name we just stored. However, the syntax that we are going to see is very good at it’s working as it allows us to do both tasks (asking the name and greeting the person) in a single statement. Use this technique as shown in the following snippet:

Sign Up and Start Learning