Python 包装代码而不添加新行(反斜杠)

Python wrap code without adding a new line (backslash)

所以我有这段代码要包装,我一直在寻找解决方案,大多数人说使用'\'但是当我从异常中打印 msg 时,它用换行符拆分字符串

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

打印为:

There was a 
connect issue

我想包装代码但将其输出为一行,我该怎么做。谢谢

只需将每行的字符串用引号引起来 这将起作用:

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

使用隐式字符串连接。

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

两个相邻的字符串文字将合并为一个字符串。这两个文字可以出现在不同的行;由于 Python 括号内的隐式行继续,因此不计算中间空格。