Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly break a long line in Python?

Tags:

python

How would I go about breaking the following line? The PEP8 guideline doesn't make it very clear to me.

confirmation_message = _('ORDER_CREATED: %(PROPERTY_1)s - %(PROPERTY_2)s - %(PROPERTY_3)s - %(PROPERTY_4)s')  % {'PROPERTY_1': order.lorem, 'PROPERTY_2': order.ipsum, 'PROPERTY_4': order.dolor, 'PROPERTY_5': order.sit}
like image 404
Virgiliu Avatar asked May 08 '15 12:05

Virgiliu


People also ask

How do you break long code lines?

1.9 Breaking long linesWith the option -l n , or --line-length n , it is possible to specify the maximum length of a line of C code, not including possible comments that follow it. When lines become longer than the specified line length, GNU indent tries to break the line at a logical place.

How do I make a line break in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.


1 Answers

One would typically do something like:

confirmation_message = _(
    'ORDER_CREATED: %(PROPERTY_1)s - '
    '%(PROPERTY_2)s - %(PROPERTY_3)s - %(PROPERTY_4)s') % {
        'PROPERTY_1': order.lorem,
        'PROPERTY_2': order.ipsum,
        'PROPERTY_3': order.ipsum,
        'PROPERTY_4': order.dolor,
        'PROPERTY_5': order.sit
    }

This takes advantage of the fact that adjacent strings ('like ' 'this') are concatenated to shorten the long line of text; everything else is split on commas.

like image 55
larsks Avatar answered Sep 30 '22 06:09

larsks