如果我的字符串列表太长,如何跳转到python中的下一行?

2024-09-28 03:19:10 发布

您现在位置:Python中文网/ 问答频道 /正文

第1行

names_boys = ["john", "paul", "peter", "roger", "pete", "johnson", "derick", "christof", "andrew"]

#如果这个列表很长,我想继续下一行(第2行),而不是一行很长的代码,我该怎么办

我尝试过使用“\n”,但什么都没有

请容忍我。我是python新手


Tags: 代码列表namesjohnpeter新手johnsonandrew
3条回答

Python statements are usually written in a single line. The newline character marks the end of the statement. If the statement is very long, we can explicitly divide into multiple lines with the line continuation character (\).

Python supports multi-line continuation inside parentheses ( ), brackets [ ], and braces { }. The brackets are used by List and the braces are used by dictionary objects. We can use parentheses for expressions, tuples, and strings.

根据第二条规则,您可以将行拆分为

names_boys = [
    "john", "paul", "peter", "roger", 
    "pete", "johnson", "derick", "christof", 
    "andrew"]

您可以沿着一行走下去,或者在所需位置使用\,如下所示:

names_boys = ["john", "paul", "peter", "roger", "pete",\
              "johnson", "derick", "christof", "andrew"]

请记住\之后不能有任何空白或注释

您可以执行以下简单操作:

names_boys = ["john", "paul", "peter", "roger", "pete",
              "johnson", "derick", "christof", "andrew"]

相关问题 更多 >

    热门问题