Multiple line rules

I’m always leary putting things on multiple lines because I thought there were only certain circumstances where that could be done, like a dictionary for example. In line of syntax I thought a \n was needed to split, as you say, to a new line.

I’m very green at this and get mixed up on the rules of one thing to another. I’m starting to get worried my 47yr old brain isn’t up to this but I really like the problem solving and that Ah-ha moment when I figure it out.

Nearly always, an error or exception is just a minor annoyance. Especially a SyntaxError, which occurs before your code can run. So be bold and feel safe that if you get the syntax wrong, the interpreter will tell you.

I don’t remember what all the rules for multi-line code are, but I think these are the most important:

Multi-line strings: use triple quote marks “”" or ‘’’ create multi-line strings.

Brackets – round () square and curly {} – can extend over multiple lines. Not just dicts, but also lists, tuples, sets, function calls, list comprehensions, etc.

And finally, if all else fails (and this should be a last resort) you can extend a single line of code over multiple physical lines by ending it with a backslash:

some_really_long_name = \


But beware! You can’t have anything after the backslash, not even a space. So its best to avoid this and use the bracket/parentheses trick instead:

some_really_long_name = some(



1 Like

My brain is 60 years old, and what works for me often is to read the documentation. In this case, The Python Language Reference, 2. Lexical analysis.

The rules are quite simple, actually.

A Python program is divided into a number of logical lines.

2.1.1. Logical lines

The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.…

2.1.5. Explicit line joining

Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character.…

2.1.6. Implicit line joining

Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes.…

In the post you quoted, @vbrozik connected readability to splitting across lines:

For a demonstration of how line-splitting can add to readability I encourage to you play with Black, the uncompromising code formatter. I don’t claim that you will want to break lines in Black’s way, but it is an interesting point of view, and I am happy submitting to it. There is a Black playground which seems to let you try things without installing Black itself. See how and where it adds physical line breaks. Do you understand why those line breaks are syntactically valid? Do you think it adds to readability?

In the playground link provided, I do see the line breaks on the left side and I understand why they are valid. The right side, however is easier to read and adding parenthesis around things so that you can use multiple lines without the line-breaks seems much better.

After the Marines in the 90’s I’ve mostly worked non-technical jobs so my knowledge of the inner working of computers dates back to C: commands with a floppy disk. I can use a computer well enough for the jobs I do and I taught myself to type, so I feel like some basics classes in computer math or computer logic might help my comprehension of the logic behind the code I’m writing. I know this is an emerging field and I like the problem solving aspect of what I’ve learned so far… I’m also not sure which career field would best suit my skills once I can get some certifications. Where are the guidance counselors we used to have when we were kids lol. I’m hoping to find a good networking group in my community that can offer some guidance. Thank you for your insight!

Ah, so you like how Black breaks lines! Just to be clear, the left side of the Black playground is the input code, and the right side is how Black formats it. Note that the right side has more lines. That is because Black added line breaks to make the code clearer.

Also, note that you can type or paste your own code into the left side, and the playground will show you what Black makes of it in the right side. Try typing in the new_stuff = [] expression above, and see how Black inserts line breaks.