Search code examples
pythonprintingformattingline-breaks

Can I use line breaks in a python format funtion print line?


This is the line of code I want to break up, it works fine but it is a very long line, how does one use line breaks in a format function print line like this?

return '{0.name} is a {0.sex} rabbit and has {0.fur_colour} fur and {0.eye_colour} eyes, {0.name} is of the {0.breed} breed of rabbits.\n{0.name} is '.format(paul)+ disp_age

Solution

  • You could store your whole splitted string into ()

    return ("{0.name} is a {0.sex} rabbit"
        " and has {0.fur_colour} fur and "
        "{0.eye_colour} eyes, {0.name} is "
        "of the {0.breed} breed of rabbits.\n"
        "{0.name} is ").format(paul)+ disp_age
    

    This answer about multi-line strings was useful: https://stackoverflow.com/a/10660443/7042357