📜  在Python中将长行分成多行

📅  最后修改于: 2022-05-13 01:55:20.859000             🧑  作者: Mango

在Python中将长行分成多行

断行与输出无关,它只是试图修复我们的代码的显示方式。在一行中编写很长的一行会使代码看起来不那么干净,并且有可能将其混淆为复杂。分解同一行可以增加代码的可读性,排除任何混乱,并且显然可以使其具有代表性。通常,一行经过一定数量的字符后会进行分割。本文讨论了实现这一目标的所有方法:

方法 1:使用反斜杠

可以在行之间放置一个反斜杠 (\) 以使其看起来分开,如下所示。另请注意,所有三种情况都产生完全相同的输出,唯一的区别在于它们在代码中的呈现方式:

例子:

Python3
print("BEFORE BREAKING:")
print("How many times were you frustrated while looking out for a good collection of programming/ algorithm/ interview questions? What did you expect and what did you get? Geeks for geeks is a portal that has been created to provide well written, well thought and well explained solutions for selected questions.")
  
print()
print("AFTER BREAKING:")
print("How many times were you frustrated while looking out "\
      "for a good collection of programming/ algorithm/ "\
      "interview questions? What did you expect and what "\
      "did you get? Geeks for geeks is a portal that"\
      " has been created to provide well written, we"\
      "ll thought and well explained solutions for se"\
      "lected questions.")
  
  
print()
line = "How many times were you frustrated while looking out "\
    "for a good collection of programming/ algorithm/ "\
    "interview questions? What did you expect and what "\
    "did you get? Geeks for geeks is a portal that"\
    " has been created to provide well written, we"\
    "ll thought and well explained solutions for se"\
    "lected questions."
print("AFTER BREAKING USING A VARIABLE:")
print(line)


Python3
print("How many times were you" +
      " frustrated while looking" +
      " out for a good collection" +
      " of programming/ algorithm/" +
      "interview questions? What" +
      " did you expect and what " +
      "did you get? Geeks for gee" +
      "ks is a portal that has bee" +
      "n created to provide well wr" +
      "itten, well thought and wel" +
      "l explained solutions for se" +
      "lected questions.")


Python3
print(("How many times were you"),
      ("frustrated while looking"),
      ("out for a good collection"),
      ("of programming/ algorithm/"),
      ("interview questions? What"),
      ("did you expect and what"),
      ("did you get? Geeks for geeks"),
      ("is a portal that has been"),
      ("created to provide well"),
      ("written, well thought and well"),
      ("explained solutions for"),
      ("selected questions."))


输出:

方法二:使用字符串连接运算符

字符串连接运算符(+),如此基本的东西可以轻松替换上面示例中的反斜杠以给出相同的输出。

例子:

蟒蛇3

print("How many times were you" +
      " frustrated while looking" +
      " out for a good collection" +
      " of programming/ algorithm/" +
      "interview questions? What" +
      " did you expect and what " +
      "did you get? Geeks for gee" +
      "ks is a portal that has bee" +
      "n created to provide well wr" +
      "itten, well thought and wel" +
      "l explained solutions for se" +
      "lected questions.")

输出:

方法 3:使用括号

通过将每个片段放在括号中并使用逗号(,)将每个片段彼此分开,可以实现相同的输出。

例子:

蟒蛇3

print(("How many times were you"),
      ("frustrated while looking"),
      ("out for a good collection"),
      ("of programming/ algorithm/"),
      ("interview questions? What"),
      ("did you expect and what"),
      ("did you get? Geeks for geeks"),
      ("is a portal that has been"),
      ("created to provide well"),
      ("written, well thought and well"),
      ("explained solutions for"),
      ("selected questions."))

输出: