Python long line print

How to create a long multi-line string in Python?

The first approach is by using triple quotes, we should add a triple quote then we can add separate line inside the quotes, the multi-line string is created.

Using triple quotes in Python, it is possible to span strings across many lines. It can also be applied to lengthy code comments. Triple quotes can also contain special characters like TAB, verbatim, or NEWLINES. As the name would imply, the syntax consists of three single or double quotes placed consecutively.

Example

In the program given below, we are taking a multi−line string as input and we are printing it −

str1 ="""Welcome to Tutorialspoint How are you doing? Hope everything is fine Thank you """ print("The multi-line string is") print(str1)

Output

The output of the above example is as shown below −

The multi-line string is Welcome to Tutorialspoint How are you doing? Hope everything is fine Thank you

Using parenthesis

The second approach is by using parenthesis and multiple double quotes. We have to include the entire string inside the parenthesis and after completion of each line we should add new line character (\n) and add new line.

Читайте также:  Java check if folder exists

Example

In the example given below, we are taking a multi-line string by using parenthesis and printing it −

str1 =("Welcome to Tutorialspoint\n" "How are you doing?\n" "Hope everything is fine\n" "Thank you") print("The multi-line string is") print(str1)

Output

The output of the above example is as shown below −

The multi-line string is Welcome to Tutorialspoint How are you doing? Hope everything is fine Thank you

Using \ and newline character

The third approach is by using \ and newline character. This is similar to the above approach but we are not including the parenthesis we include \ after each new line character.

Example

In the example given below, we are taking a multi-line string by using parenthesis and printing it. −

str1 ="Welcome to Tutorialspoint\n"\ "How are you doing?\n"\ "Hope everything is fine\n"\ "Thank you" print("The multi-line string is") print(str1)

Output

The output of the above example is given below −

The multi-line string is Welcome to Tutorialspoint How are you doing? Hope everything is fine Thank you

Источник

Write a long string on multiple lines in Python

In Python, when using PEP8 code checkers like flake8, an E501 line too long error is raised when one line exceeds 80 characters.

This article explains how to split a long string over multiple lines without including a newline character.

See the following article for various operations related to strings with line breaks.

If you want to wrap or truncate long strings, the textwrap module is useful.

If a line becomes too long due to method chaining, you can break up the line similarly.

Line continuation character in Python: backslash ( \ )

In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, the line is considered to continue on the next line.

Furthermore, if multiple string literals are written sequentially, they are concatenated into one string as follows:

s = 'aaa' 'bbb' print(s) # aaabbb 

Therefore, you can break up a long string into multiple lines as follows:

s = 'https://ja.wikipedia.org/wiki/'\ '%E3%83%97%E3%83%AD%E3%82%B0%E3%83'\ '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E' print(s) # https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E 

Note that only string literals (strings enclosed by ‘ or » ) are concatenated when written consecutively. Writing variables consecutively without an operator will raise an error.

s_var = 'xxx' # s = 'aaa' s_var 'bbb' # SyntaxError: invalid syntax 

Use the + operator to concatenate variables, or variables and string literals.

s = 'aaa' + s_var + 'bbb' print(s) # aaaxxxbbb 

The + operator is required to concatenate variables, even if they are separated by a backslash ( \ ).

s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\ + s_var\ + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' print(s) # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

For more details on string concatenation, see the following article:

Use parentheses for line continuation

In Python, you can freely break lines inside parentheses ( () , <> , [] ). Using this rule, you can split a long string across multiple lines using parentheses instead of backslashes.

Since <> is used for sets and [] is used for lists, use () for this purpose. Note that tuples are created by commas, not () .

s = ('https://ja.wikipedia.org/wiki/' '%E3%83%97%E3%83%AD%E3%82%B0%E3%83' '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E') print(s) # https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E 

If variables are included, you need the + operator.

s_var = 'xxx' s = ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + s_var + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb') print(s) # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

Источник

Write a long string on multiple lines in Python

In Python, when using PEP8 code checkers like flake8, an E501 line too long error is raised when one line exceeds 80 characters.

This article explains how to split a long string over multiple lines without including a newline character.

See the following article for various operations related to strings with line breaks.

If you want to wrap or truncate long strings, the textwrap module is useful.

If a line becomes too long due to method chaining, you can break up the line similarly.

Line continuation character in Python: backslash ( \ )

In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, the line is considered to continue on the next line.

Furthermore, if multiple string literals are written sequentially, they are concatenated into one string as follows:

s = 'aaa' 'bbb' print(s) # aaabbb 

Therefore, you can break up a long string into multiple lines as follows:

s = 'https://ja.wikipedia.org/wiki/'\ '%E3%83%97%E3%83%AD%E3%82%B0%E3%83'\ '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E' print(s) # https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E 

Note that only string literals (strings enclosed by ‘ or » ) are concatenated when written consecutively. Writing variables consecutively without an operator will raise an error.

s_var = 'xxx' # s = 'aaa' s_var 'bbb' # SyntaxError: invalid syntax 

Use the + operator to concatenate variables, or variables and string literals.

s = 'aaa' + s_var + 'bbb' print(s) # aaaxxxbbb 

The + operator is required to concatenate variables, even if they are separated by a backslash ( \ ).

s = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\ + s_var\ + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' print(s) # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

For more details on string concatenation, see the following article:

Use parentheses for line continuation

In Python, you can freely break lines inside parentheses ( () , <> , [] ). Using this rule, you can split a long string across multiple lines using parentheses instead of backslashes.

Since <> is used for sets and [] is used for lists, use () for this purpose. Note that tuples are created by commas, not () .

s = ('https://ja.wikipedia.org/wiki/' '%E3%83%97%E3%83%AD%E3%82%B0%E3%83' '%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E') print(s) # https://ja.wikipedia.org/wiki/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0%E8%A8%80%E8%AA%9E 

If variables are included, you need the + operator.

s_var = 'xxx' s = ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + s_var + 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb') print(s) # aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaxxxbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 

Источник

How to Break Long Lines in Python

Python supports implicit line continuation. This means you can break long lines of code.

For example, instead of this:

math_students = ["Alice", "Bob", "Charlie", "David", "Emmanuel"]

You can write it like this:

math_students = [ "Alice", "Bob", "Charlie", "David", "Emmanuel" ]

As a matter of fact, you should limit all lines of code to a maximum of 79 characters.

Today, you are going to learn when and how to break long lines in Python.

Breaking Long Lines of Code in Python

Breaking lines increases the total number of lines of code. But at the same, it can drastically improve the readability of your code.

It is recommended not to have lines of code longer than 79 characters.

Python supports implicit line continuation. This means any expression inside the parenthesis, square brackets, or curly braces can be broken into multiple lines.

For example, here is a long print() function call is broken into multiple lines in a couple of ways:

print("Alice", "Bob", "Charlie", "David", "Emmanuel", "Farao", "Gabriel", "Hilbert", "Isaac") print( "Alice", "Bob", "Charlie", "David", "Emmanuel", "Farao", "Gabriel", "Hilbert", "Isaac" ) print( "Alice", "Bob", "Charlie", "David", "Emmanuel", "Farao", "Gabriel", "Hilbert", "Isaac" )

There are many ways you can break long lines in Python. You may want to check the official style guide that explains best practices more thoroughly.

Before jumping into examples, notice that there is a way to automatize the line-breaking process.

How to Auto-Break Long Lines of Code in Python

Popular code editors allow you install plugins that enforce code style guidelines.

A cool feature of these plugins is you can usually auto-format code. In other words, the plugin automatically takes care no line exceeds the 79 character “limit”.

For example, in VSCode it is possible to automatically format code on save.

Next up, let’s see some examples when you may want to split expressions into multiple lines.

How to Break a String into Multiple Lines

To break a string to multiple lines, wrap each string in the new line inbetween a pair of double quotation marks.

For instance, you can do this:

print( "This happens to be" " so long string that" " it may be a better idea" " to break the line not to" " extend it further to the right" )

But you cannot do this:

print( "This happens to be so long string that it may be a better idea to break the line not to extend it further to the right" )

Break a Function Arguments into Multiple Lines

If your function takes a number of arguments that extends line of code far to the right, feel free to break the expression into multiple lines.

For example, instead of doing this:

def example_function(first_number, second_number, third_number, fourth_number, fifth_number): pass
def example_function( first_number, second_number, third_number, fourth_number, fifth_number ): pass

Break a List into Multiple Lines

For example, let’s create a 3×3 matrix using a list:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This is fine, but as you may know, a matrix is written in a table format. This makes it look more like a table of values.

To follow this convention in Python, you can split the matrix (the list of lists) into multiple lines:

matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

Break a Dictionary into Multiple Lines

Just like breaking a list declaration into multiple lines, you can do it with a dictionary.

For instance, instead of a long expression like this:

Let’s split the dictionary into a bunch of lines to make it more understandable:

Break Mathematical Operations into Multiple Lines

To split a chain of binary operations, break the line before the operator. This makes the code more readable as the binary operators are not all over the place.

For example, instead of doing this:

income = gross_wages + taxable_interest + (dividends - qualified_dividends) - ira_deduction - student_loan_interest
income = (gross_wages + taxable_interest + (dividends - qualified_dividends) - ira_deduction - student_loan_interest)

This example is directly from the PEP-0008 style guide.

Break Comparisons into Multiple Lines

Like any other expression, comparisons can also take some space. To avoid too long chains of comparisons, you can break the line.

if (is_rainy == True and is_hot == True and is_sunny == True and is_night == True): print("How is that possible. ")

Conclusion

Python’s implicit continuation makes it possible to break long expressions into multi-line expressions. This is useful for code readability.

To break an expression into multiple lines, wrap the expression around a set of parenthesis and break it down as you want.

If the expression is already in a set of parenthesis, square brackets, or curly braces, you can split it to multiple lines. This is true for example for lists, tuples, and dictionaries.

Thanks for reading. I hope you like it.

Источник

Оцените статью