12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

136 Chapter 14. Files>>> line1 = "This here's the wattle,\n">>> fout.write(line1)Again, the file object keeps track of where it is, so if you call write again, it adds the new data totheend.>>> line2 = "the emblem of our land.\n">>> fout.write(line2)When you are done writing, you have toclosethe file.>>> fout.close()14.3 FormatoperatorThe argument of write has to be a string, so if we want to put other values in a file, we have toconvert them tostrings. The easiest way todo that iswithstr:>>> x = 52>>> f.write(str(x))Analternativeistousetheformatoperator,%. Whenappliedtointegers,%isthemodulusoperator.But when thefirstoperand is astring,%isthe formatoperator.Thefirstoperandistheformatstring,whichcontainsoneormoreformatsequences,whichspecifyhow the second operand isformatted. The result isastring.For example, the format sequence'%d' means that the second operand should be formatted as aninteger (dstands for “decimal”):>>> camels = 42>>> '%d' % camels'42'The resultisthe string'42',which isnot tobeconfused withthe integer value42.A format sequence can appear anywhere inthe string,soyou can embed avalue inasentence:>>> camels = 42>>> 'I have spotted %d camels.' % camels'I have spotted 42 camels.'If there is more than one format sequence in the string, the second argument has to be a tuple. Eachformat sequence ismatched withanelement ofthe tuple, inorder.The following example uses '%d' to format an integer, '%g' to format a floating-point number(don’t ask why), and'%s' toformatastring:>>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')'In 3 years I have spotted 0.1 camels.'The number of elements in the tuple has to match the number of format sequences in the string.Also, thetypes of the elements have tomatch theformat sequences:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!