Home Lesson-3.5

Lesson-3.4

Formatted printing

Consider the following program:

When this code is executed with Sachin as the input, we get the following output:

This looks messy as there is an unwanted space after the name. This is a formatting issue. Python provides some useful tools to format text the way we want.

 

f-strings

The first method that we will look at is called formatted string literals or f-strings for short. Let us jump into the syntax:

When this code is executed with Sachin as the input, we get the following output:

The messy formatting has been corrected. Let us take a closer look at the string inside the print command:

This is called a formatted string literal or f-string. The f in front of the string differentiates f-strings from normal strings. f-string is an object which when evaluated results in a string. The value of the variable name is inserted in place of {name} in the f-string. Two things are important for f-strings to do our bidding:

Let us see what happens if we miss one of these two:

This will give the output:

Let us now look at few other examples:

For l = 4, b = 5, the output is:

Going back to the code, lines 2 and 3 are quite clear. Notice that line-4 has an expression — l * b — inside the curly braces and not just a variable. f-strings allow any valid Python expression inside the curly braces. If the f-string has some {expression} in it, the interpreter will substitute the value of expression in the place of {expression}. Another example:

For an input of 3, this will give the following result:

The \t is a tab character. It has been added before and after the =. Remove both the tabs and run the code. Do you see any change in the output?

Till now we have used f-strings within the print statement. Nothing stops us from using it to define other string variables:

Try to guess what this code is doing.

 

format()

Another way to format strings is using a string method called format().

In the above string, the curly braces will be replaced by the value of the variable name. Another example:

Let us now print the multiplication table using format:

The output will be identical to the one we saw when we used f-strings. Some points to note in line-3 of this code-block. There are three pairs of curly braces. The values that go into these three positions are given as three arguments in the format function. Starting from the left, the first pair of curly braces in the string is replaced by the first argument in format, the second pair by the second argument and so on. Few more examples:

First, consider the following code:

In this code, the mapping is implicit. The first pair of curly braces is mapped to the first argument and so on. This can be made explicit by specifying which argument a particular curly braces will be mapped to:

The integer inside the curly braces gives the index of the argument in the format function. The arguments of the format function are indexed from 0 and start from the left. Changing the order of arguments will change the output. A third way of writing this as follows:

This method uses the concept of keyword arguments which we will explore in the lessons on functions in the next chapter. Until then, let us put this last method on the back-burner.

 

Format specifiers

Consider the following code:

This gives the following output:

There are too many numbers after the decimal point. In many real world applications, having two or at most three places after the decimal point is sufficient. In fact, having as many as fifteen numbers after the decimal point only confuses readers. Format specifiers are a way to solve this problem:

This gives the following output:

Let us look at the content inside the curly braces: {pi_approx:.2f}. The first part before the : is the variable. Nothing new here. The part after : is called a format specifier. .2f means the following:

Let us consider a variant of this code:

This gives the following output:

Let us now take another example. Let us say we want to print the marks of three students in a class:

This gives the following output:

While this is not bad, we would like the marks to be right aligned and have a uniform representation for the marks. This is what we wish to see:

This is much more neater. The following code helps us achieve this:

The part that might be confusing is the second curly braces in each of the print statements. Let us take a closer look: {marks_1:10.2f}. The part before the : is the variable. The part after the : is 10.2f. Here again, .2f signifies that the float value should be rounded off to two decimal places. The 10 before the decimal point is the minimum width of the column used for printing this value. If the number has fewer than 10 characters (including the decimal point), this will be compensated by adding spaces before the number.

For a better understanding of this concept, let us turn to printing integers with a specific formatting. This time, we will use the format function:

This gives the following output:

Points to note in the code: