[Tutor] How to wrap this line of code?

Alan Gauld alan.gauld at btinternet.com
Wed Aug 23 09:48:47 CEST 2006


"Dick Moores" <rdm at rcblue.com> wrote
>I have this long print statement in a script:
>
> print "Initial integer of first sequence with number of terms of %d
> or more was %s  (%d)" % (length, intCommas(n_for_max_c), 
> n_for_max_c)
>
> It's one line of code, and too long. How can I wrap it so that it is
> written in the code as 2 lines?

Bob has answered the specific but here's another thing to consider:

Since its too long for your code its probably too long for your
display too, so think about where you would like the line break
to be on the final print. Put in a newline character there and
put the line break there.

Alternatively use triple quoting to format the string nicely and
then use the variable in the print statement:

longstr = """
Initial integer of first sequence with
number of terms of %d or more was %s  (%d)
"""

print longstr % (length, intCommas(n_for_max_c), n_for_max_c)

HTH,


-- 
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