Previous Lecture lect11 Next Lecture

lect11, Wed 02/20

String Formats and File I/O

Slides PDF

The text file, itsybitsy.txt, that was used in this lesson can be copied from THIS SITE.

###############################
print(42, end="")
print(43)
print(44)

hour = 12
minute = 55
second = 31

print('{1} hrs {0} min {2} sec'.format(hour, minute,second))
print('{} hrs {} min {} sec'.format(hour, minute,second))

###############################
a = "Hemoglobin"
print('{:7}'.format(a))

###############################
n = 33.33333333
print('{:9.3f}'.format(n))

###############################
outfile = open('Z_Output.txt', 'w')

outfile.write("Hello! This is a new file I just made!\n")

outfile.close()

###############################
infile = open('itsybitsy.txt', 'r')

listOfLines = infile.readlines()

infile.close()

for line in listOfLines:
    print(line, "***")