Input and Output
Console Input
The console is a text interface for input to and output from the computer. In Spyder, the iPython window itself can serve as a console. In Jupyterlab the output console is indicated by lines marked with Out []
whereas a reference to an input console will open a textbox.
To read input from the console we use the input()
function (Python 3+). In Python 2.7 the equivalent is raw_input()
. Any string within the parentheses is optional and, if present, if will be printed to prompt the user. The input from the user is captured as a string and returned; it must be stored for any subsequent use.
The input function returns only a string. If you need to use the values as any other type, you must perform the conversion yourself.
Example:
weight=input("Enter your weight in pounds:")
print(type(weight))
weight=float(input("Enter your weight in pounds:"))
print(type(weight))
Console Output
We have already been using the print
function. Let us now examine it in more detail.
The print function
- always inserts a space between its arguments
- always adds a newline character unless the
end
argument is added.- print(var,end="")
Messages can be added in between variables.
h=1.45;
w=62.
print("Your BMI is",w/ht**2)
Exercise
Use Spyder or another IDE to write a complete program to compute BMI from weight and height input from a user. First request the user’s choice of units. We have not spent much time with strings yet so you may use a digit to indicate the user’s choice, but remember it will still be a string on input. Then request weight and height. You will need to convert these from strings. Look up the correct conversion factors for Imperial to metric units. Compute the BMI.
The categories are
BMI | Category |
---|---|
less than 18.5 | Underweight |
18.5 to 25.0 | Normal |
25.0 to 30.0 | Overweight |
30.0 to 35.0 | Obese Class I |
35.0 to 40.0 | Obese Class II |
more than 40.0 | Obese Class III |
Print the user’s BMI value and category.
Example solution
unit=int(float(input("Enter 1 for Imperial or 2 for metric units:")))
if unit==1:
weight=float(input("Enter your weight in pounds:"))
height=float(input("Enter your height in inches:"))
BMI=weight*703.1/height**2
elif unit==2:
weight=float(input("Enter your weight in kg:"))
height=float(input("Enter your height in m:"))
BMI=weight/height**2
else:
print("Invalid unit request")
BMI=None
if BMI < 18.5 : category="Underweight"
if 18.5 <= BMI < 25.0 : category="Normal"
if 25.0 <= BMI < 30.0 : category="Overweight"
if 30.0 <= BMI < 35.0 : category="Obese Class I"
if 35.0 <= BMI < 40.0 : category="Obese Class II"
if 40.0 <= BMI < 45.0 : category="Obese Class III"
if BMI>= 45.0 : category="Obese Class IV"
print("Your BMI is {:.2f} and you are {}".format(BMI,category))
Formatted Output
So far we have only considered list-directed input/output. We leave it to the interpreter to format. However, we often want or need more control over the format. For this we define format strings.
Formats are indicated by a pattern
F.wf
F.wg
F.we
F is the field width (total number of columns occupied), w is the number of digits to the right of the decimal point; these must be substituted by numbers. The letters f, g, and e are called format codes and indicate floating-point (decimal point) format, general format (interpreter decides f or e), and exponential notation. If we do not specify w for numbers, the default is 6 decimal digits printed. If we specify the number of digits the interpreter will round the result; if unspecified it will truncate.
Examples:
- Use exactly 15 spaces with 8 decimal places
- 15.8f
- Let the interpreter decide the total field width but use 8 decimal places
- .8f
- Print a float as an integer (this will truncate, not round, the decimal places)
- 0f
- Print in scientific notation with three decimal places
- .3e
Strings are specified with the letter s
. They do not take a field width but can be adjusted or zero padded by using more advanced operations on the string to be printed.
Integers are specified by d
(this stands for decimal, as in base-10 number) with an optional field width. If the field width is wider than the integer, it will be padded with blanks by default. If it is narrower it will be ignored. If omitted the interpreter will use the width of the integer.
To pad with zeros, write the zero in front of the width specifier.
Examples:
5d
05d
Formatters
To construct our formatted output we use format strings, also called formatters. We insert curly braces as placeholders for variables in the finished string. Format codes go inside the braces following a colon.
print("The value of pi is approximately {:.6f}".format(math.pi))
print("Pi to {:d} digits is {:.12f}".format(n,math.pi)
We have left the space to the left of the colon blank, but it represents the order of the arguments to format
. Thus the second example is equivalent to
print("Pi to {0:d} digits is {1:.12f}".format(n,math.pi))
This means we can rearrange the output if we wish.
print("Pi is {1:.12f} to {0:d} digits".format(n,math.pi))
Empty curly braces result in default formatting and ordering. In this situation one could use list-directed formatting as well, but using a formatter enables more control over layout and spacing.
print("I have {} books to read by {}".format(12,"tomorrow"))
Values can be aligned in a field with <
(left align), >
(right align), or ^
(center align). These symbols must be followed by a field width.
print("The number {:^8} is {:>15.4f}".format("pi",math.pi))
Exercise
Type at the interpreter
import math
Practice printing math.pi
- Print without any formatting
- Print using the default f format
- Print to 5 decimal places
- Print the integer part
- For at least one of the above, add a message
- Print the number of digits to at least 6 spaces and pad with zeros
- Print the number with a message that specifies the number of digits, where the number of digits is also to be printed.
Even more sophisticated formatting is possible. In the above examples, when printing pi the value of n
had to be 12 or the output would be inconsistent. In newer Python versions we can make the width a variable.
- print(“Pi to {} digits is .{dec}f}".format(math.pi,dec=n))
Formatting with f-Strings
For Python 3.6 and up the formatted string literal or f-string was introduced. These can be used to create formatted output easily.
Example
n=12
print(f"Pi to {n} places is {math.pi:.{n}f}")
The f-string evaluates the contents of the curly braces when the program is run. That means that expressions can be used inside the curly braces.
print(f"Pi to {n//3} places is {math.pi:.{n//3}f}")
In this example, the integer division is required because the result of the expression must be an integer.
If quotation marks are needed inside an f-string (such as for a dictionary key) they must be single quotes. In addition, the backslash \
cannot be a character within an f-string, so if a character such as the newline \n
is needed, it must be assigned to a variable.
Resources
Full documentation is here. Details on format strings is here.
A good reference for f-strings is here.