How can I break up this long line in Python?

Question:

How would you go about formatting a long line such as this? I’d like to get it to no more than 80 characters wide:

logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title))

Is this my best option?

url = "Skipping {0} because its thumbnail was already in our system as {1}."
logger.info(url.format(line[indexes['url']], video.title))
Asked By: Gattster

||

Answers:

Consecutive string literals are joined by the compiler, and parenthesized expressions are considered to be a single line of code:

logger.info("Skipping {0} because it's thumbnail was "
  "already in our system as {1}.".format(line[indexes['url']],
  video.title))

Personally I dislike hanging open blocks, so I’d format it as:

logger.info(
    'Skipping {0} because its thumbnail was already in our system as {1}.'
    .format(line[indexes['url']], video.title)
)

In general I wouldn’t bother struggle too hard to make code fit exactly within a 80-column line. It’s worth keeping line length down to reasonable levels, but the hard 80 limit is a thing of the past.

Answered By: bobince

That’s a start. It’s not a bad practice to define your longer strings outside of the code that uses them. It’s a way to separate data and behavior. Your first option is to join string literals together implicitly by making them adjacent to one another:

("This is the first line of my text, "
"which will be joined to a second.")

Or with line ending continuations, which is a little more fragile, as this works:

"This is the first line of my text, " 
"which will be joined to a second."

But this doesn’t:

"This is the first line of my text, "  
"which will be joined to a second."

See the difference? No? Well you won’t when it’s your code either.

(There’s a space after in the second example.)

The downside to implicit joining is that it only works with string literals, not with strings taken from
variables, so things can get a little more hairy when you refactor. Also, you can only interpolate formatting on the combined string as a whole.

Alternatively, you can join explicitly using the concatenation operator (+):

("This is the first line of my text, " + 
"which will be joined to a second.")

Explicit is better than implicit, as the zen of python says, but this creates three strings instead of one, and uses twice as much memory: there are the two you have written, plus one which is the two of them joined together, so you have to know when to ignore the zen. The upside is you can apply formatting to
any of the substrings separately on each line, or to the whole lot from outside the parentheses.

Finally, you can use triple-quoted strings:

"""This is the first line of my text
which will be joined to a second."""

This is often my favorite, though its behavior is slightly different as the newline and any leading whitespace on subsequent lines will show up in your final string. You can eliminate the newline with an escaping backslash.

"""This is the first line of my text 
which will be joined to a second."""

This has the same problem as the same technique above, in that correct code only differs from incorrect code by invisible whitespace.

Which one is "best" depends on your particular situation, but the answer is not simply aesthetic, but one of subtly different behaviors.

Answered By: jcdyer

You can use textwrap module to break it in multiple lines

import textwrap
str="ABCDEFGHIJKLIMNO"
print("n".join(textwrap.wrap(str,8)))

ABCDEFGH
IJKLIMNO

From the documentation:

textwrap.wrap(text[, width[, …]])
Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

Optional keyword arguments correspond to the instance attributes of TextWrapper, documented below. width defaults to 70.

See the TextWrapper.wrap() method for additional details on how wrap() behaves.

Answered By: Saurabh

For anyone who is also trying to call .format() on a long string, and is unable to use some of the most popular string wrapping techniques without breaking the subsequent .format( call, you can do str.format("", 1, 2) instead of "".format(1, 2). This lets you break the string with whatever technique you like. For example:

logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title))

can be

logger.info(str.format(("Skipping {0} because its thumbnail was already"
+ "in our system as {1}"), line[indexes['url']], video.title))

Otherwise, the only possibility is using line ending continuations, which I personally am not a fan of.

Answered By: Simon Alford

Solution without extra packages load:

def split_by_len(txt: str, l: int, sep: str or None='n') -> str or list:
    """
    txt: str text
    l: split length (symbols per split)
    sep: separate string or None for list of strs
    """
    spl_list = [txt[i * l : i * l + l] for i in range(len(txt) // l + 1)]
    return spl_list if sep==None else sep.join(spl_list)

Example 1:

print(split_by_len(txt='XXXXX', l=2, sep='n'))

XX
XX
X

Example 2:

print(split_by_len(txt='XXXXX', l=2, sep=' '))

XX XX X

Example 3:

print(split_by_len(txt='XXXXX', l=2, sep=None))

['XX', 'XX', 'X']
Answered By: George Shimanovsky