Break a long line into multiple lines in Python - GeeksforGeeks

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.
python-basics
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Defaultdict in Python
Python OOPs Concepts
Python | os.path.join() method
Python | Pandas dataframe.groupby()