[Tutor] Style help: long strings with formatting
Ricardo Aráoz
ricaraoz at gmail.com
Wed Jul 30 15:03:25 CEST 2008
Alan Gauld wrote:
>
> "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, you have a couple of \n that the OP did not ask for.
Wayne, besides Alan's suggestion you might be looking for something
like :
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)
or :
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)
Or whatever. Parenthesis will help a lot when formatting your code.
HTH
More information about the Tutor
mailing list