Printing Information

In Writing and Running a Program, built-in function print was used to print values to the screen. We will use print to print messages to the users of our program. Those messages may include the values that expressions produce and the values that the variables refer to. Here are two examples of printing:

 >>>​​ ​​print(1​​ ​​+​​ ​​1)
 2
 >>>​​ ​​print(​​"The Latin 'Oryctolagus cuniculus' means 'domestic rabbit'."​​)
 The Latin 'Oryctolagus cuniculus' means 'domestic rabbit'.

Function print doesn’t allow any styling of the output: no colors, no italics, no boldface. All output is plain text.

The first function call does what you would expect from the numeric examples we have seen previously, but the second does something slightly different from previous string examples: it strips off the quotes around the string and shows us the string in a human-readable form, rather than its character representation. This example makes the difference between the two even clearer:

 >>>​​ ​​print(​​'In 1859, Charles Darwin revolutionized biology'​​)
 In 1859, Charles Darwin revolutionized biology
 >>>​​ ​​print(​​'and our understanding of ourselves'​​)
 and our understanding of ourselves
 >>>​​ ​​print(​​'by publishing "On the Origin of Species".'​​)
 by publishing "On the Origin of Species".

And the following example shows that when Python prints a string, any escape sequences are converted to a form that humans expect:

 >>>​​ ​​print(​​'one two three four'​​)
 one two
 three four

The previous example shows how the tab character can be used to lay values out in columns.

In Creating a Multiline String, we saw that indicates a new line in multiline strings. When a multiline string is printed, those sequences are displayed as new lines:

 >>>​​ ​​numbers​​ ​​=​​ ​​''​​'​​one
 ...​​ ​​two
 ...​​ ​​three​​''​​'
 >>>​​ ​​numbers
 'one two three'
 >>>​​ ​​print(numbers)
 one
 two
 three

Function print takes a comma-separated list of values and prints the values with a single space between them and a newline after the last value:

 >>>​​ ​​print(1,​​ ​​2,​​ ​​3)
 1 2 3
 >>>

When called with no arguments (which is a comma-separated list of length zero), print ends the current line, advancing to the next one:

 >>>​​ ​​print()
 
 >>>

Function print can print values of any type, and it can even print values of different types in the same function call:

 >>>​​ ​​print(1,​​ ​​'two'​​,​​ ​​'three'​​,​​ ​​4.0)
 1 two three 4.0

As with other function calls, it is also possible to call print with an expression as an argument. It will print the value of that expression:

 >>>​​ ​​radius​​ ​​=​​ ​​5
 >>>​​ ​​print(​​"The diameter of the circle is"​​,​​ ​​radius​​ ​​*​​ ​​2,​​ ​​"cm."​​)
 The diameter of the circle is 10 cm.

Function print has a few extra helpful features; here is the help documentation for it:

 >>>​​ ​​help(print)
 Help on built-in function print in module builtins:
 
 print(...)
  print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)
 
  Prints the values to a stream, or to sys.stdout by default.
  Optional keyword arguments:
  file: a file-like object (stream); defaults to the current sys.stdout.
  sep: string inserted between values, default a space.
  end: string appended after the last value, default a newline.
  flush: whether to forcibly flush the stream.

The parameters sep, end, file, and flush have assignment statements in the function header! These are called default parameter values: by default, if we call function print with a comma-separated list of values, the separator is a space; similarly, a newline character appears at the end of every printed string. (We won’t discuss file and flush; they are beyond the scope of this text.)

We can supply different values by using keyword arguments. (In the Python documentation, these are often referred to explicitly as kwargs.) That’s a fancy term for assigning a value to a parameter name in the function call. Here, we separate each value with a comma and a space instead of just a space by including sep=’, ’ as an argument:

 >>>​​ ​​print(​​'a'​​,​​ ​​'b'​​,​​ ​​'c'​​)​​ # The separator is a space by default
 a b c
 >>>​​ ​​print(​​'a'​​,​​ ​​'b'​​,​​ ​​'c'​​,​​ ​​sep=​​', '​​)
 a, b, c

Often you’ll want to print information but not start a new line. To do this, use the keyword argument end=” to tell Python to end with an empty string instead of a new line:

 >>>​​ ​​print(​​'a'​​,​​ ​​'b'​​,​​ ​​'c'​​,​​ ​​sep=​​', '​​,​​ ​​end=​​''​​)
 a, b, c>>>

Notice how the last prompt appeared right after the ’c’. Typically, end=” is used only in programs, not in the shell. Here is a program that converts three temperatures from Fahrenheit to Celsius and prints using keyword arguments:

 def​ convert_to_celsius(fahrenheit: float) -> float:
 """ Return the number of Celsius degrees equivalent to fahrenheit degrees.
 
  >>> convert_to_celsius(75)
  23.88888888888889
  """
 
 return​ (fahrenheit - 32.0) * 5.0 / 9.0
 
 print​(​'80, 78.8, and 10.4 degrees Fahrenheit are equal to '​, end=​''​)
 print​(convert_to_celsius(80), end=​', ​​ ​​'​)
 print​(convert_to_celsius(78.8), end=​', and '​)
 print​(convert_to_celsius(10.4), end=​' Celsius.​​ ​​'​)

Here’s the output of running this program:

 80, 78.8, and 10.4 degrees Fahrenheit are equal to 26.666666666666668,
 26.0, and -12.0 Celsius.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.159.223