简体   繁体   English

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

[英]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 谢谢

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. 由于括号内Python的隐式行连续性,因此不计算中间空格。

Just enclose the strings with quotes on each line This shall work: 只需在每行上用引号将字符串引起来,这将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM