Skip to content
Related Articles

Related Articles

Break a long line into multiple lines in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Jul, 2022

Break a long line into multiple lines in Python, 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.

Example:

Long Line: a = 1 + 2 + 3 + 4 - 5 * 2

Multiple Lines: b = 1 \
                     + 2 + \
                       3 + 4\
                     - 5 *\
                      2

Method 1: Break a long line into multiple lines using backslash

A backslash(\) can be put between the line to make it appear separate as shown below. Also, notice that all three cases produce exactly the same output only difference is in the way they are presented in 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?")
 
print()
print("AFTER BREAKING:")
print("How many times were you frustrated while looking out "\
      "for a good collection of programming/ algorithm/ "\
      "interview questions?")
 
 
print("\n")
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"
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?

AFTER BREAKING:

How many times were you frustrated while looking out for a good collection of programming/ algorithm/ interview 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.

Method 2: Break a long line into multiple lines using the string concatenation operator

The string concatenation operator (+), something so basic can easily replace backslashes in the above example to give out the 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: Break a long line into multiple lines using parenthesis

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

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.

Comparing three double quotes and three single quotes 

In this example, we will try to compare the 2 multiline strings in Python, to check if both are the same or not. As in the output, we can see that we are getting False because x is detecting a newline character (\n) in line x, whereas in y there is no newline character.

Python3




x = '''Geeks
for
geeks'''
 
y = """Geeks for geeks"""
 
x1 = '''Geeks for geeks'''
 
y1 = """Geeks for geeks"""
 
print(x==y)
print(x1==y1)

Output:

False
True

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!