Skip to content
Related Articles

Related Articles

Improve Article

Break a long line into multiple lines in Python

  • Last Updated : 23 Dec, 2020

Breaking a long line has nothing to do with the output, it is just trying to fix how our code appears. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Breaking down the same line can increase the readability of the code, rule out any confusion, and obviously makes it presentable. Usually, a line undergoes division after it crosses a certain amount of characters. This article discusses all the ways in which this can be achieved:

Method 1: Using backslash

A backslash(\) can be put between the line to make it appear separate like shown below. Also notice that all three cases produce exactly the same output only difference is in the way they are presented into the code:

Example:

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)

Output:



BEFORE BREAKING:

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.

AFTER BREAKING:

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.

AFTER BREAKING USING A VARIABLE:

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.

Method 2: Using string concatenation operator

String concatenation operator (+), something so basic can easily replace backslashes in the above example to give out same output.



Example:

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.")

Output:

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.

Method 3: Using parenthesis

The same output can be achieved by keeping each fragment into parentheses and separating each fragment from each other using comma(,).

Example:

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."))

Output:

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :