1.10. Input and Output

1.10.1. The input Function

The hello program of The Classic First Program always does the same thing. This is not very interesting. Programs are only going to be reused if they can act on a variety of data. One way to get data is directly from the user. Modify the hello.py program as follows in the editor, and save it with File ‣ Save As....`, using the name hello_you.py.

person = input('Enter your name: ')
print('Hello', person)

Run the program. In the Shell you should see

Enter your name:

Follow the instruction (and press Enter). Make sure the typing cursor is in the Shell window, at the end of this line. After you type your response, you can see that the program has taken in the line you typed. That is what the built-in function input does: First it prints the string you give as a parameter (in this case 'Enter your name: '), and then it waits for a line to be typed in, and returns the string of characters you typed. In the hello_you.py program this value is assigned to the variable person, for use later.

The parameter inside the parentheses after input is important. It is a prompt, prompting you that keyboard input is expected at that point, and hopefully indicating what is being requested. Without the prompt, the user would not know what was happening, and the computer would just sit there waiting!

Open the example program, interview.py. Before running it (with any made-up data), see if you can figure out what it will do:

'''Illustrate input and print.'''

applicant = input("Enter the applicant's name: ")
interviewer = input("Enter the interviewer's name: ")
time = input("Enter the appointment time: ")
print(interviewer, "will interview", applicant, "at", time)

The statements are executed in the order they appear in the text of the program: sequentially. This is the simplest way for the execution of the program to flow. You will see instructions later that alter that natural flow.

If we want to reload and modify the hello_you.py program to put an exclamation point at the end, you could try:

person = input('Enter your name: ')
print('Hello', person, '!')

Run it and you see that it is not spaced right. There should be no space after the person’s name, but the default behavior of the print function is to have each field printed separated by a space. There are several ways to fix this. You should know one. Think about it before going on to the next section. Hint: [1]

[1]The + operation on strings adds no extra space.

1.10.3. Numbers and Strings of Digits

Consider the following problem: Prompt the user for two numbers, and then print out a sentence stating the sum. For instance if the user entered 2 and 3, you would print ‘The sum of 2 and 3 is 5.’

You might imagine a solution like the example file addition1.py, shown below. There is a problem. Can you figure it out before you try it? Hint: [2]

'''Error in addition from input.'''

x = input("Enter a number: ")
y = input("Enter a second number: ")
print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='') #error

End up running it in any case.

We do not want string concatenation, but integer addition. We need integer operands. Briefly mentioned in Whirlwind Introduction To Types and Functions was the fact that we can use type names as functions to convert types. One approach would be to do that. Further variable names are also introduced in the example addition2.py file below to emphasize the distinctions in types. Read and run:

'''Conversion of strings to int before addition'''

xString = input("Enter a number: ")
x = int(xString)
yString = input("Enter a second number: ")
y = int(yString)
print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')

Needing to convert string input to numbers is a common situation, both with keyboard input and later in web pages. While the extra variables above emphasized the steps, it is more concise to write as in the variation in example file, addition3.py, doing the conversions to type int immediately:

'''Two numeric inputs, with immediate conversion'''

x = int(input("Enter a number: "))
y = int(input("Enter a second number: "))
print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='')

The simple programs so far have followed a basic programming pattern: input-calculate-output. Get all the data first, calculate with it second, and output the results last. The pattern sequence would be even clearer if we explicitly create a named result variable in the middle, as in addition4.py

'''Two numeric inputs, explicit sum'''

x = int(input("Enter an integer: "))
y = int(input("Enter another integer: "))
sum = x+y
print('The sum of ', x, ' and ', y, ' is ', sum, '.', sep='')

We will see more complicated patterns, which involve repetition, in the future.

[2]The input function produces values of string type. Exercise for Addition

Write a version, add3.py, that asks for three numbers, and lists all three, and their sum, in similar format to addition4.py displayed above. Exercise for Quotients

Write a program, quotient.py, that prompts the user for two integers, and then prints them out in a sentence with an integer division problem like

The quotient of 14 and 3 is 4 with a remainder of 2

Review Division and Remainders if you forget the integer division or remainder operator.

1.10.4. String Format Operation

In grade school quizzes a common convention is to use fill-in-the blanks. For instance,

Hello _____!

and you can fill in the name of the person greeted, and combine given text with a chosen insertion. We use this as an analogy: Python has a similar construction, better called fill-in-the-braces. There is a particular operation on strings called format, that makes substitutions into places enclosed in braces. For instance the example file, hello_you3.py, creates and prints the same string as in hello_you2.py from the previous section:

'''Hello to you!  Illustrates format with {} in print.

person = input('Enter your name: ')
greeting = 'Hello, {}!'.format(person)

There are several new ideas here!

First method calling syntax for objects is used. You will see this very important modern syntax in more detail at the beginning of the next chapter in Object Orientation. All data in Python are objects, including strings. Objects have a special syntax for functions, called methods, associated with the particular type of object. In particular str objects have a method called format. The syntax for methods has the object followed by a period followed by the method name, and further parameters in parentheses.


In the example above, the object is the string 'Hello {}!'. The method is named format. There is one further parameter, person.

The string for the format method has a special form, with braces embedded. Places where braces are embedded are replaced by the value of an expression taken from the parameter list for the format method. There are many variations on the syntax between the braces. In this case we use the syntax where the first (and only) location in the string with braces has a substitution made from the first (and only) parameter.

In the code above, this new string is assigned to the identifier greeting, and then the string is printed.

The identifier greeting was introduced to break the operations into a clearer sequence of steps. However, since the value of greeting is only referenced once, it can be eliminated with the more concise version:

person = input('Enter your name: ')
print('Hello {}!'.format(person))

Consider the interview program. Suppose we want to add a period at the end of the sentence (with no space before it). One approach would be to combine everything with plus signs. Another way is printing with keyword sep=''. Another approach is with string formatting. Using our grade school analogy, the idea is to fill in the blanks in

_____ will interview _____ at _____.

There are multiple places to substitute, and the format approach can be extended to multiple substitutions: Each place in the format string where there is '{}', the format operation will substitute the value of the next parameter in the format parameter list.

Run the example file interview2.py, and check that the results from all three methods match.

'''Compare print with concatenation and with format string.'''

applicant = input("Enter the applicant's name: ")
interviewer = input("Enter the interviewer's name: ")
time = input("Enter the appointment time: ")
print(interviewer + ' will interview ' + applicant + ' at ' + time +'.')
print(interviewer, ' will interview ', applicant, ' at ', time, '.', sep='')
print('{} will interview {} at {}.'.format(interviewer, applicant, time))

Sometimes you want a single string, but not just for printing. You can combine pieces with the + operator, but then all pieces must be strings or explicitly converted to strings. An advantage of the format method is that it will convert types to string automatically, like the print function. Here is another variant of our addition sentence example, addition4a.py, using the format method.

'''Two numeric inputs, explicit sum'''

x = int(input("Enter an integer: "))
y = int(input("Enter another integer: "))
sum = x+y
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum)

Conversion to strings was not needed in interview2.py. (Everything started out as a string.) In addition4a.py, however, the automatic conversion of the integers to strings is useful.

So far there is no situation that requires a format string instead of using other approaches. Sometimes a format string provides a shorter and simpler expression. Except where specifically instructed in an exercise for practice, use whatever approach to combining strings and data that you like. There are many elaborations to the fields in braces to control formatting. We will look at one later, String Formats for Float Precision, where format strings are particularly useful.

A technical point: Since braces have special meaning in a format string, there must be a special rule if you want braces to actually be included in the final formatted string. The rule is to double the braces: '{{' and '}}'. The example code formatBraces.py, shown below, makes setStr refer to the string 'The set is {5,9}.'. The initial and final doubled braces in the format string generate literal braces in the formatted string:

'''Illustrate braces in a formatted string.'''

a = 5
b = 9
setStr = 'The set is {{{}, {}}}.'.format(a, b)

This kind of format string depends directly on the order of the parameters to the format method. There is another approach with a dictionary, that was used in the first sample program, madlib.py, and will be discussed more in Dictionaries and String Formatting. The dictionary approach is probably the best in many cases, but the count-based approach is an easier start, particularly if the parameters are just used once, in order.

Optional elaboration with explicitly numbered entries

Imagine the format parameters numbered in order, starting from 0. In this case 0, 1, and 2. The number of the parameter position may be included inside the braces, so an alternative to the last line of interview2.py is (added in example file interview3.py):

print('{0} will interview {1} at {2}.'.format(interviewer, applicant, time))

This is more verbose than the previous version, with no obvious advantage. However, if you desire to use some of the parameters more than once, then the approach with the numerical identification with the parameters is useful. Every place the string includes '{0}', the format operation will substitute the value of the initial parameter in the list. Wherever '{1}' appears, the next format parameter will be substituted....

Predict the results of the example file arith.py shown below, if you enter 5 and 6. Then check yourself by running it. In this case the numbers referring to the parameter positions are necessary. They are both repeated and used out of order:

'''Fancier format string example with 
parameter identification numbers
-- useful when some parameters are used several times.'''

x = int(input('Enter an integer: '))
y = int(input('Enter another integer: '))
formatStr = '{0} + {1} = {2}; {0} * {1} = {3}.'
equations = formatStr.format(x, y, x+y, x*y)

Try the program with other data.

Now that you have a few building blocks, you will see more exercises where you need to start to do creative things. You are encouraged to go back and reread Learning to Problem-Solve. Addition Format Exercise

Write a version of Exercise for Addition, add3f.py, that uses the string format method to construct the same final string as before. Quotient Format Exercise

Write a version of the quotient problem in Exercise for Quotients, quotientformat.py, that uses the string format method to construct the same final string as before. Again be sure to give a full sentence stating both the integer quotient and the remainder.