[Tutor] Style help: long strings with formatting

Alan Gauld alan.gauld at btinternet.com
Tue Jul 29 19:21:12 CEST 2008


"W W" <srilyk at gmail.com> wrote

> output = "At an average weekly savings of $%.02f, your monthly 
> savings  will
> be $%.02f. \n Your annual savings will be $%.02f." % (diff, 
> monthly_savings,
> annual_savings)
> print output.
>
> As you can see, it's very much longer than the 72 characters 
> suggested in
> the PEP 8 found here: http://www.python.org/dev/peps/pep-0008/

Use a triple quoted string for multiline output

output = """
At an average weekly savings of \t$%.02f,
your monthly savings  will be     \t$%.02f.
Your annual savings will be       \t$%.02f.
""" % (diff, monthly_savings, annual_savings)

print output


> I know it would be fairly trivial to split it up into several 
> strings, and
> concatenating or printing each one.

Multiline strings are designed to save you having to do that.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list