Search code examples
pythonword-wrap

Python wrap code without adding a new line (backslash)


So I have this code that i want to wrap, and i have looked for solutions, most people say to use '\' but when i print the msg from the exception, it splits the string with the new line character

raise specialExceptions.ConnectError("There was a \
                                      connect issue")

this prints as:

There was a 
connect issue

I want to wrap the code but output it as one line, how do i do this. thanks


Solution

  • Use implicit string concatenation.

    raise specialExceptions.ConnectError("There was a "
                                         "connect issue")
    

    Two string literals appearing adjacent to one another will be merged into a single string. The two literals can appear on different lines; intervening whitespace is not counted, due to Python's implicit line continuation inside parentheses.