Prints - formatting

We have been using print functions such as print for a while already. In this section we will cover some advanced tools we can use to improve our print statements. We have saved this for now as it requires some knowledge we have built up to this point.

In it's most basic form, a print statement is a function that prints what is passed to it. There are additional parameters this function accepts.

In the following code editor we include the character \n. When the backslash symbol (\, called the escape character) is parsed by the interpreter, it can perform specific pre-written actions. In the case of \n it means to make a new line and will perform that action rather than print the text \n. Similarly, this goes as well for \0, which is a null character and \t, which is a tab. If we want to actually print this character then all one needs to do is prefix another backslash, so \\t will print the character \t.

What if we want to print some variables inside a print statement? There are quite a number of ways to do it. The first is with use of commas, the next is use of format, and the third is with string literal. A string literal has the character f placed right before the first quotation.

As we see, all three methods produce the same output; it's really a matter of preference on how one wants to structure their print statements. If we print with use of commas as separator, then it will concat the strings and variables together with a space separator. The use of format will pass the arguments linearly into spaces where curly brackets are located in the string. This requires an equal number of curly brackets for variables. A string literal is very similar to format, except it allow us to place the variables we want to print within curly brackets.

We can also format numbers however we like. We can round a number or pad a number among other options. With use of string literals, optional formatting is possible via a colon. We can use a number, or a decimal number to indicate the minimum number of values to display.

In the first example we round the number to one decimal place. The f character within a bracket is optional, but usually helpful to know that the input will end up a float number. In the second example we left pad the numbers with spaces. This tells the interpreter to make this variable a minimum of this number of spaces wide. If the number is larger, then it will print normally.

We see in the second example use of d didn't do anything as the variable is already an integer, then we see use of f convert an integer to a float with a single decimal place. Since it takes a total of three characters there's still room for one space. The third number is larger than it's minimum size and so it ignores this part. There is also use of shorthand to print the variable and an equal sign followed by the variable itself as shown in b; this is called a self-documenting expression.

We can also do math and call functions within a print statement. These expressions will execute before the statement is formatted. This is sometimes helpful when we want to simply check the response of a function without saving the result to a variable.

We see both ways of printing end up with the same result, so it's really up the user. One more thing to note, is the use of the sep argument. It separates inputs that are passed in by comma with that separation. It defaults to \s, that is a single space, so we could instead, do a tab or new line as well as no space as we did here.

Practice Question

Select the choice that is produced from the following print statement.

score: 0%