2017-06-06 35 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