0

It works only if I'm doing a print statement for some reason. For example, I have

warnings.warn('blabla\ 
continuing writing') 

where continuing is on the next line in the IDE. It gives me an error yet it's the same line continuation as always. Why is this happening?

masque
  • 177
  • 7
  • 2
    Possible duplicate of [Python style - line continuation with strings?](https://stackoverflow.com/questions/5437619/python-style-line-continuation-with-strings) – joel Aug 08 '18 at 15:47
  • @JoelBerkeley I don't think it's a dup. While that answer does show alternate ways of accomplishing a similar thing, it doesn't explain what's wrong with this code, which is what the question is asking. And they also don't accomplish the _same_ thing—the OP's code should have a newline in the string, but those answers show how to _avoid_ getting a newline in the string. – abarnert Aug 08 '18 at 15:52
  • This question is pretty confusing as written. If you could edit the question to show examples that do and don’t work, explain what “doesn’t work” means (if it’s an exception, paste the whole thing), and explain the desired behavior, it may turn out that this is a dup after all, of you may need a different answer from the one I wrote. – abarnert Aug 08 '18 at 16:37

1 Answers1

1

At least as posted here, you have a space after the backslash.

A backslash continuation is a backslash followed by a newline. A backslash followed by a space followed by a newline is just a backslash-escaped space, and an unescaped newline.

This is, of course, hard to see in most text editors, and on sites like Stack Overflow (I had to click "edit" on your question and scroll the cursor around to check), which is one of the reasons many people avoid backslash continuations whenever possible.

And it's especially a problem using them in strings, because a backslash-escaped space is a perfectly legal thing to put in a string, so the compiler won't give you an error about it, and even most linters won't warn about it.

If you can configure your editor to recognize backslashed whitespace and display it in some way, or maybe just to show you all newlines with some subtle marker, it may be helpful.

A better solution—assuming you actually want a newline there—is to use triple quotes:

warnings.warn('''blabla 
continuing writing''')

… possibly with textwrap.dedent so you can line things up nicely:1

from textwrap import dedent as D
warnings.warn(D(
    '''blabla 
    continuing writing'''))

… or to use an explicit \n:

warnings.warn('blabla\ncontinuing writing')

… possibly together with string concatenation:

warnings.warn('blabla\n'
              'continuing writing')

(Depending on the purpose of this output, it also might be worth considering not worrying about exactly where the newlines go and letting textwrap.fill take care of making it fit the output.)


1. However, it's worth noting that the examples in the textwrap docs actually use backslash-escaped newlines to feed to dedent, allowing you to put the triple-quote on one line and the start of the text on the next...

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thank you. The triple quotes worked. I actually didn't have a space in my original code (my bad), but it still wasn't working. I actually don't want a new line in the standard output. I just want to continue writing the statement on the next line in the IDE because it would get too long. – masque Aug 08 '18 at 16:32
  • @masque In that case, you probably just want string concatenation, and your question _is_ a duplicate of the one Joel Berkeley found (which has nice answers covering all the options). – abarnert Aug 08 '18 at 16:35