In trying to obey the python style rules, I've set my editors to a max of 79 cols. Show
In the PEP, it recommends using python's implied continuation within brackets, parentheses and braces. However, when dealing with strings when I hit the col limit, it gets a little weird. For instance, trying to use a multiline
Will return
This works:
Since it returns this:
But, when the statement is indented a few blocks in, this looks weird:
If you try and indent the second line:
Your string ends up as:
The only way I've found to get around this is:
Which I like better, but is also somewhat uneasy on the eyes, as it looks like there is a string just sitting in the middle of nowhere. This will produce the proper:
So, my question is - what are some people's recommendations on how to do this and is there something I'm missing in the style guide that does show how I should be doing this? Thanks.
Introduction to Python line continuationWe cannot split a statement into multiple lines in Python by just pressing Enter. Instead, most of the time we use the backslash ( \ ) to indicate that a statement is continued on the next line. In this tutorial, we will learn how we can show the continuation of a line in Python using different techniques. We will see why we cannot use triple, double, or single quotation marks for the line continuation. At the same time, we will cover different ways of line continuation including backslash, and brackets to show the continuation of a line in Python by taking various examples. In a nutshell, this tutorial contains all the details and methods that you need to learn in order to know how to perform line break Getting started with Python line continuationThe preferred way of wrapping long lines is by using python's parentheses. These should be used in preference to using a backslash for line continuation. The line continuation operator, \ can be used to split long statements over multiple lines. In python, a backslash (\) is a continuation character, and if it is placed at the end of a line, it is considered that the line is continued, ignoring subsequent newlines. First, let us see what problems occur when we try to just quotation marks. Problems with single, double, and triple quotation marksIn Python, quotation marks are used to define a string class. Something written inside quotation marks will be considered as a string. If we will write long sentences inside these quotation marks without explicitly defining any line continuation operator, and just press enters to go to the next line, the program will give an error. In the following sections, we will take different examples and see what kind of errors we get with the continuation of a line within quotation marks. Examples of problems with single and double marksNow let us take an example and see what kind of error we get when we go the next line inside quotation marks without explicitly defining any line continuation operator. See the example below:
Output: File "/home/uca/Downloads/python/main.py", line 2
mystring = "Wellcome to
^
SyntaxError: EOL while scanning string literal Notice that we get an error because the quotation marks have to end in one line. We cannot start quotation marks in one line and end in another line. The same case is with single quotation marks. See the example below: Notice that again we get the error when we use the single quotation marks. Example of problem with triple quotation marksYou have noticed that we cannot write multiple lines of text inside double and single quotation marks without any explicitly defining line continuation operator. In the case, of triple quotation marks, the program will not return any error, in fact, the output will be in different lines rather than being in one line only. See the example below:
Output: Wellcome to
golinuxcloud,
here you can find
programming tutorials Although, this method does not return any error but the output is not the one that we require. We need the output to be line one line even if we wrote the string in different lines. For that purpose, we have to explicitly define the line continuation operators inside our string. In the following sections, we will cover some of them. Python line continuation with explicit line break operatorThe backslash Examples with explicit line break operatorNow let us take an example and see how we can use the break operator for the Python line continuation. See the examples below:
Output: Wellcome to golinux, here you can find programming tutorials
Wellcome to golinux, here you can find programming tutorials Notice that this time we didn't get any error and the output is in one line because of the line continuation operator that we have used. The same output will be for the triple quotation marks. See the example below:
Output: Wellcome to golinux, here you can find programming tutorials Notice that even we have used the triple quotation marks, but still the output is in one line because of the line continuation operator. Examples of integers and float with explicit line break operatorSo far we have taken examples of strings only. Let us take an example of integers and add numbers together from a different line. See the example below:
Output: 15 Notice that we were able to add the numbers that were in the different lines by using the python line continuation operator. The same will be applied for floating points as well. See the example below:
Output: 39.60000000000001 Notice that the output is also a floating number that was obtained by adding different numbers from different lines. Python line continuation with brackets()Another method that can be used for the python line continuation is to enclose the lines inside Python line continuations of strings using bracketsNow let us take an example and see how we can use brackets for the line continuation in Python. See the example below:
Output: welcome to golinuxcloud programming tutorials Notice that by using brackets and the addition operator, we were able to write multiple lines of strings in one line as an output. Python line continuation of integers and floats using bracketsNow let us take an example of integers datatype and see how we can use the brackets to add the numbers from different lines. See the python program below:
Output: 48 Notice that we get the sum of the numbers that were in different lines. The same is with the floating numbers. See the example below:
Output: 39.60000000000001 Notice that a sum is again a floating number. SummarySuppose that we have a long line that is difficult to read, so we want to split it into multiple lines without actually creating new lines. In Python, we have different techniques to perform line continuation. In this tutorial, we have learned about the kind of errors that can occur if we just use quotation marks and write a long sentence in multiple lines. Then we discussed two different ways to perform break long lines using the backslash operator which is also called line continuation operator and the brackets '()' which can also be used for similar purpose in Python by taking various examples. Further ReadingPython line continuation How do you go to the next line in Python?The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
How do you write multiple lines on a string in Python?Use triple quotes to create a multiline string
It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.
How do you break a long line in Python?To break a line in Python, use the parentheses or explicit backslash(/). Using parentheses, you can write over multiple lines. The preferred way of wrapping long lines is using Python's implied line continuation inside parentheses, brackets, and braces.
How do you add a line break in string?In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
|