If you have a statement in Python that is too long and you want to break that statement into multiple lines without breaking the indentation rule of Python, then you need to take a look at the PEP 8 which is titled "Style Guide for Python Code"
To break a line without breaking the indentation rule, you can use line continuation techniques.
Example:
Let's say we have a string with a concatenation of a few strings.
my_data = "This is some text" + " that is too long " + " and breaks readability" + " as it spans over 79 characters."+ " Lets try to fix it"
We can make use of below-line continuation techniques
Option 1: Using parentheses for line continuation
my_data = ("This is some text" +
" that is too long " +
" and breaks readability" +
" as it spans over 79 characters." +
" Let's try to fix it")
Option 2: Using backslashes for line continuation
my_data = "This is some text" + \
" that is too long " + \
" and breaks readability" + \
" as it spans over 79 characters." + \
" Let's try to fix it"
Option 3: Implicit line continuation (no plus sign needed)
my_data = "This is some text" \
" that is too long " \
" and breaks readability" \
" as it spans over 79 characters." \
" Let's try to fix it"
Example:
>>> my_data = "This is some text" \
... " that is too long " \
... " and breaks readability" \
... " as it spans over 79 characters." \
... " Let's try to fix it"
>>> print(my_data)
This is some text that is too long and breaks readability as it spans over 79 characters. Let's try to fix it
>>>

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!