Input and Output

In this section, we describe printing, which includes the use of the print function, the old-style % format specifiers and the new style {} format specifiers.

Printing to standard output (normally the screen)

The print function is the most commonly used command to print information to the “standard output device” which is normally the screen.

There are two modes to use print.

Simple print

The easiest way to use the print command is to list the variables to be printed, separated by comma. Here are a few examples:

In [1]:
a = 10
b = 'test text'
In [2]:
test text
In [3]:
print(a, b)
10 test text
In [4]:
print("The answer is", a)
The answer is 10
In [5]:
print("The answer is", a, "and the string contains", b)
The answer is 10 and the string contains test text
In [6]:
print("The answer is", a, "and the string reads", b)
The answer is 10 and the string reads test text

Python adds a space between every object that is being printed.

Python prints a new line after every print call. To suppress that, use the end= parameter:

In [7]:
print("Printing in line one", end='')
print("...still printing in line one.")
Printing in line one...still printing in line one.

Formatted printing

The more sophisticated way of formatting output uses a syntax very similar to Matlab’s fprintf (and therefor also similar to C’s printf).

The overall structure is that there is a string containing format specifiers, followed by a percentage sign and a tuple that contains the variables to be printed in place of the format specifiers.

In [8]:
print("a = %d b = %d" % (10,20))
a = 10 b = 20

A string can contain format identifiers (such as %f to format as a float, %d to format as an integer, and %s to format as a string):

In [9]:
from math import pi
print("Pi = %5.2f" % pi)
Pi =  3.14
In [10]:
print("Pi = %10.3f" % pi)
Pi =      3.142
In [11]:
print("Pi = %10.8f" % pi)
Pi = 3.14159265
In [12]:
print("Pi = %d" % pi)
Pi = 3

The format specifier of type %W.Df means that a Float should be printed with a total Width of W characters and D digits behind the Decimal point. (This is identical to Matlab and C, for example.)

To print more than one object, provide multiple format specifiers and list several objects in the tuple:

In [13]:
print("Pi = %f, 142*pi = %f and pi^2 = %f." % (pi,142*pi,pi**2))
Pi = 3.141593, 142*pi = 446.106157 and pi^2 = 9.869604.

Note that the conversion of a format specifier and a tuple of variables into string does not rely on the print command:

In [14]:
from math import pi
"pi = %f" % pi
'pi = 3.141593'

This means that we can convert objects into strings whereever we need, and we can decide to print the strings later – there is no need to couple the formatting closely to the code that does the printing.

Overview of commonly used format specifiers using the astronomical unit as an example:

In [15]:
AU = 149597870700  # astronomical unit [m]
"%f" % AU        # line 1 in table
specifier style Example output for AU
%f floating point 149597870700.000000
%e exponential notation 1.495979e+11
%g shorter of %e or %f 1.49598e+11
%d integer 149597870700
%s str() 149597870700
%r repr() 149597870700L

“str” and “__str__”

All objects in Python should provide a method __str__ which returns a nice string representation of the object. This method a.__str__() is called when we apply the str function to object a:

In [16]:
a = 3.14
In [17]:

The str function is extremely convenient as it allows us to print more complicated objects, such as

In [18]:
b = [3, 4.2, ['apple', 'banana'], (0, 1)]
"[3, 4.2, ['apple', 'banana'], (0, 1)]"

The way Python prints this is that it uses the __str__ method of the list object. This will print the opening square bracket [ and then call the __str__ method of the first object, i.e. the integer 3. This will produce 3. Then the list object’s __str__ method prints the comma , and moves on to call the __str__ method of the next element in the list (i.e. 4.2) to print itself. This way any composite object can be represented as a string by asking the objects it holds to convert themselves to strings.

The string method of object x is called implicitly, when we

  • use the “%s” format specifier to print x

  • pass the object x directly to the print command:

In [19]:
[3, 4.2, ['apple', 'banana'], (0, 1)]
In [20]:
print("%s" % b)
[3, 4.2, ['apple', 'banana'], (0, 1)]

“repr” and “__repr__”

A second function, repr, should convert a given object into a string presentation so that this string can be used to re-created the object using the eval function. The repr function will generally provide a more detailed string than str. Applying repr to the object x will attempt to call x.__repr__().

In [21]:
from math import pi as a1
In [22]:
In [23]:
number_as_string = repr(a1)
a2 = eval(number_as_string)  # evaluate string 
In [24]:
a2-a1                      # -> repr is exact representation
In [25]:
In [26]:
a1-eval(str(a1))           # -> str has lost a few digits

We can convert an object to its str() or repr presentation using the format specifiers %s and%r, respectively.

In [27]:
import math
"%s" % math.pi
In [28]:
"%r" % math.pi

New-style string formatting

A new system of built-in formatting allows more flexibility for complex cases, at the cost of being a bit longer.

Basic ideas in examples:

In [29]:
"{} needs {} pints".format('Peter', 4)     # insert values in order
'Peter needs 4 pints'
In [30]:
"{0} needs {1} pints".format('Peter', 4)   # index which element
'Peter needs 4 pints'
In [31]:
"{1} needs {0} pints".format('Peter', 4)
'4 needs Peter pints'
In [32]:
"{name} needs {number} pints".format(    # reference element to
    name='Peter',number=4)               # print by name
'Peter needs 4 pints'
In [33]:
"Pi is approximately {:f}.".format(math.pi)     # can use old-style format options for float
'Pi is approximately 3.141593.'
In [34]:
"Pi is approximately {:.2f}.".format(math.pi)   # and precision
'Pi is approximately 3.14.'
In [35]:
"Pi is approximately {:6.2f}.".format(math.pi)  # and width
'Pi is approximately   3.14.'

Changes from Python 2 to Python 3: print

One (maybe the most obvious) change going from Python 2 to Python 3 is that the print command loses its special status. In Python 2, we could print “Hello World” using:

print "Hello world"             # valid in Python 2.x

Effectively, we call the function print with the argument Hello World. All other functions in Python are called such that the argument is enclosed in parentheses, i.e.

In [36]:
print("Hello World")               # valid in Python 3.x
Hello World

This is the new convention required in Python 3 (and allowed for recent version of Python 2.x.)

Everything we have learned about formatting strings using the percentage operator still works the same way:

In [37]:
import math
a = math.pi
"my pi = %f" % a           # string formatting
'my pi = 3.141593'
In [38]:
print("my pi = %f" % a)    # valid print in 2.7 and 3.x
my pi = 3.141593
In [39]:
"Short pi = %.2f, longer pi = %.12f." % (a, a)
'Short pi = 3.14, longer pi = 3.141592653590.'
In [40]:
print("Short pi = %.2f, longer pi = %.12f." % (a, a))
Short pi = 3.14, longer pi = 3.141592653590.
In [41]:
print("Short pi = %.2f, longer pi = %.12f." % (a, a))
Short pi = 3.14, longer pi = 3.141592653590.
In [42]:
# 1. Write a file
out_file = open("test.txt", "w")          #'w' stands for Writing
out_file.write("Writing text to file. This is the first line.\n"+\
               "And the second line.")
out_file.close()                          #close the file
# 2. Read a file
in_file = open("test.txt", "r")           #'r' stands for Reading
text =                     #read complete file into 
                                          #string variable text
in_file.close()                           #close the file
# 3. Display data
Writing text to file. This is the first line.
And the second line.

Reading and writing files

Here is a program that

  1. writes some text to a file with name test.txt,

  2. and then reads the text again and

  3. prints it to the screen.

The data stored in the file test.txt is:

Writing text to file. This is the first line.
And the second line.

In more detail, you have opened a file with the open command, and assigned this open file object to the variable out_file. We have then written data to the file using the out_file.write method. Note that in the example above, we have given a string to the write method. We can, of course, use all the formatting that we have discussed before—see formatted printing and new style formatting. For example, to write this file with name table table.txt we can use this Python program It is good practice to close() files when we have finished reading and writing. If a Python program is left in a controlled way (i.e. not through a power cut or an unlikely bug deep in the Python language or the operating system) then it will close all open files as soon as the file objects are destroyed. However, closing them actively as soon as possible is better style.

File reading examples

We use a file named myfile.txt containing the following 3 lines of text for the examples below:

This is the first line.
This is the second line.
This is a third and last line.
In [43]:
f = open('myfile.txt', 'w')
f.write('This is the first line.\n'
        'This is the second line.\n'
        'This is a third and last line.')

The method reads the whole file, and returns it as one string (including new line characters).

In [44]:
f = open('myfile.txt', 'r')
'This is the first line.\nThis is the second line.\nThis is a third and last line.'
In [45]:


The fileobject.readlines() method returns a list of strings, where each element of the list corresponds to one line in the string:

In [46]:
f = open('myfile.txt', 'r')
['This is the first line.\n',
 'This is the second line.\n',
 'This is a third and last line.']
In [47]:

This is often used to iterate over the lines, and to do something with each line. For example:

In [48]:
f = open('myfile.txt', 'r')
for line in f.readlines():
    print("%d characters" % len(line))
24 characters
25 characters
30 characters

Note that this will read the complete file into a list of strings when the readlines() method is called. This is no problem if we know that the file is small and will fit into the machine’s memory.

If so, we can also close the file before we process the data, i.e.:

In [49]:
f = open('myfile.txt', 'r')
lines = f.readlines()
for line in lines:
    print("%d characters" % len(line))
24 characters
25 characters
30 characters

Iterating over lines (file object)

There is a neater possibility to read a file line by line which (i) will only read one line at a time (and is thus suitable for large files as well) and (ii) results in more compact code:

In [50]:
f = open('myfile.txt', 'r')
for line in f:
    print("%d characters" % len(line))
24 characters
25 characters
30 characters

Here, the file handler f acts as in iterator and will return the next line in every subsequent iteration of the for-loop until the end of the file is reached (and then the for-loop is terminated).

Further reading

Methods of File objects, Tutorial, Section 7.2.1