Communication with outside

Table of Contents

4.1. Output
4.2. Formatting strings
4.3. Input
4.4. Files
4.4.1. Reading from a file
4.4.2. Writing into a file
4.4.3. Errors with files

4.1. Output

We saw in the previous chapter how to export information outside of the program using the print statement. Let's give a little bit more details of its use here.

The print statements can be followed by a variable number of values separated by commas. Without a value print puts only a newline character on the standard output, generally the screen. If values are provided, they are transformed into strings, and then are written in the given order, separated by a space character. The line is terminated by a newline character. You can suppress the final newline character by adding a comma at the end of the list. The following example illustrates all these possibilities:

#! /usr/local/bin/python

from string import *

dna = "ATGCAGTGCATAAGTTGAGATTAGAGACCCGACAGTA"

gc = float(count(dna, 'G') + count(dna, 'C'))/ len(dna)

print gc

print "the gc percentage of dna:", dna, "is:", gc
print
print "the gc percentage of dna:", dna
print "   is:", gc
print
print "the gc percentage of dna:", dna,
print "is:", gc

    

producing the following output:

caroline:~> python print_gc.2.py
0.432432432432
the gc percentage of dna: ATGCAGTGCATAAGTTGAGATTAGAGACCCGACAGTA is: 0.432432432432

the gc percentage of dna: ATGCAGTGCATAAGTTGAGATTAGAGACCCGACAGTA
   is: 0.432432432432

the gc percentage of dna: ATGCAGTGCATAAGTTGAGATTAGAGACCCGACAGTA is: 0.432432432432
caroline:~>