2017-06-06 51 views
0

我刚刚阅读了Python脚本的最佳实践。我发现建议的长度不应超过每行80个字符。如何添加换行符以防止过长的行?

所以在我的脚本中存在超过100个字符。

我打破了界限,下面是例子。

  • 实施例1

    self.remote_conn_pre.connect("self.hostname, " 
        "username=self.username, password=self.password, " 
        "look_for_keys=False, allow_agent=False") 
    
  • 实施例2

    self.remote_conn.send(""create bigdata\n" " 
        " .format(pending.upper(), (total) + 9)") 
    
  • 实施例3

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

如果我做错了任何事情,请帮助我解决问题,并帮助我确定将长线分成小线的最佳方法。

回答

2

您可以使用docstrings并转义换行符。

print (""" 
This is the first line of my text {} \ 
which will be joined to a second {}. 
""").format(a,b) 
0

Python支持使用反斜杠\字符分裂线,像这样:

d = "one two three " \ 
    "four five six" 

print d 

打印one two three four five six