Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Formatted Output String

In Example 3.2, typical output looked like this:

The answers are 3.0 and 4.0

(This is produced, incidentally, when the inputs to the quadratic formula are 1, –7, and 12.)

But we might like to place a period at the end, making the output read as a nice sentence. We’d like to get the following:

The answers are 3.0 and 4.0.

But the print function puts a space between each print field so that you end up getting the following, which has an unnecessary space before the last character.

The answers are 3.0 and 4.0 .

There are at least two solutions. One is to include the special sep (separator) argument to the print function. By default, print uses a single space as a separator. But we can use sep=’’ (this consists of two single quotes in a row) to indicate that print shouldn’t put in any separator at all.

This is fine, because we just take on responsibility for putting in space separators ourselves. The output statement then becomes the following:

print(’The answers are ’, x1, ’ and ’, x2, ’.’, sep=’’)

And this works, although it’s a fair amount of extra work. Not only do we have to add sep=’’, but we have to add all those extra spaces.

But there’s a better way. Python provides a way to create a formatted-output string. To use this approach, follow these steps:

  1. Create a format specification string that includes print fields denoted with the characters {}. A print field is an indication of where output characters, produced by arguments, are to be inserted into the resulting string.
  2. Apply the format method to this format-specification string, specifying the values to be printed as arguments.
  3. Print the resulting output string.

For example, you can set up a format specification string (fss) as follows:

fss = ’The numbers are {} and {}.’

Then you apply the format method to this string. The result produces an output string.

output_str = fss.format(10, 20)
print(output_str)

And here’s what the result looks like:

The numbers are 10 and 20.

Example 3.3. Distance Formula in a Script

Sooner or later, you’ll want to write and permanently save your Python programs. The steps are as follows:

  1. From within IDLE, choose the New File command from the File menu.
  2. Enter a program (or copy text) into the window that appears, which serves as a text editor for your programs.
  3. To save the program, choose Save or Save As from the File menu. The first time you save a program this way, the environment will prompt you to enter a name with a .py extension. (It will add this extension for you automatically.)
  4. To run the program, make sure the program window has the focus. Then either press F5 or select Run Module from the Run menu.
  5. After the program begins running, you may need to shift focus back to IDLE’s main window (the shell). [An exception is that with tkinter (graphical) programs, you’ll need to shift focus to the window generated by the program.]

Alternatively, you can write a program with any text editor you want, but be sure you save the file in plain-text format and give it a .py extension. Then you can load it into Python by using the Open command from IDLE’s File menu.

Although the Python environment is still extremely useful for experimenting with, and getting help with, individual commands and features, the text-editor approach is usually better for writing and executing long programs.

This next example shows how to use the Pythagorean distance formula to calculate the distance between any two points on a Cartesian plane. Here’s the formula:

distance = square_root(horizontal_dist2 + vertical_dist2)

Here’s the program listing:

  • + Share This
  • 🔖 Save To Your Account