1. Writing to standard output
    1. Writing a single line
    2. Writing lines with formatted data
      1. Formatting ordered values
      2. Formatting named values
    3. Writing escaped characters

Writing to standard output

Now you will learn to use the standard output stream. The data your program writes to standard output can be:

  • Recorded as the contents of a file
  • Displayed to a user running the program in a system console
  • Relayed as the input for another program

Since this stream is standard, you can use the same code to output data, regardless of its ultimate source.

Writing a single line

To write a line to standard output, pass the line to the print() method as a string.

Code

print('Here is the weather report for today:')
print('- It should be sunny outside')
print('- But honestly not sure')

Output

Here is the weather report for today:
- It should be sunny outside
- But honestly not sure

Each call to print() automatically adds a newline to the output stream. Adding the newline character \n adds an additional line break.

Code

print('Here is the weather report for today:\n')
print('...')
print('Sorry, no idea what it will be like.')

Output

Here is the weather report for today:

...
Sorry, no idea what it will be like.

Writing lines with formatted data

All data that gets printed must be a string. You can accomplish this by converting values to strings and/or concatenating (adding) smaller strings together.

Code

temperature = 90.7
print('The temperature outside is ' + str(temperature) + ' today.')

Output

The temperature outside is 90.7 today.

But this kind of printing can become awkward and make the message in the code harder to read.

Formatting ordered values

The fancy term for embedding data in a string is called string interpolation.

The format() method is a method that can be called on string values. This means that instead of calling the method and passing the string as an argument, you actually write the method after the string literal or variable. Formatting a string produces a new string with the inserted data. The syntax looks like this:

# Calling format on a string literal
'string {} literal'.format(value)
string_variable = 'some {} string'
string_variable.format(value)

The dot applies the method in the context of this specific string. The arguments of format() will be inserted into the new string at the locations of the {} symbols, replacing the curly braces in the order that they appear.

Code

month = 'June'
day = 3
temperature = 90.7
print('It is {} {} and it is {} degrees outside.'.format(month, day, temperature))

Output

It is June 3 and it is 90.7 degrees outside.

To change the order of appearance in the string, add the number of the argument you want inside the {} braces. Remember that argument numbering starts at 0.

Code

month = 'June'
day = 3
temperature = 90.7
print('This is day {1} of {0} and it is {2} degrees outside.'.format(month, day, temperature))

Output

This is day 3 of June 3 and it is 90.7 degrees outside.

Formatting named values

For more clarity, you may want to insert the values based on the names of the arguments to format().

Code

month = 'June'
day = 3
temperature = 90.7
message = 'This is day {day} of {month} and it is {temp} degrees outside.'
print(message.format(month=month, day=day, temp=temperature))

Output

This is day 3 of June 3 and it is 90.7 degrees outside.

Writing escaped characters

Since we surround the contents of strings in quotation marks, it becomes awkward to use quotation marks in a message.

Code

print('I love y'all')

Output

File "filename.py", line 1
    print('I love y'all')
                      ^
SyntaxError: invalid syntax

The unpaired quotation marks will cause a SyntaxError.

A simple fix is to escape the troublesome symbol. You have already seen an escaped character, the newline \n. The backslash changes it from the letter n to a line break.

Code

# added a backslash before the single quotation mark
print('I love y\'all')

Output

I love y'all