Python-How long does writing code work?

Source: Internet
Author: User
Python's coding specification requires that the length of each line is not more than 80, then there is a problem, if I really need to write a line of 80 characters above the code what to do?

Python statements can be easily implemented to divide a line into multiple lines, such as the following two types of writing are equivalent:

L = [1, 2, 3, 4, 5, 6]
L = [
1, 2, 3,
4, 5, 6
]

A line too long in a Python statement can be easily written in multiple lines, but what if I have a string length of more than 80 characters?

For example, I have a string like this:

s = ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras magna Turpis, Viverra et cursus eget, efficitur sed eros. Curabitur finibus ultricies nisi, nec convallis nunc gravida sed. Nam ac facilisis eros, quis tincidunt turpis. Vestibulum quis mi quis justo faucibus condimentum. Aliquam accumsan ultricies orci ID mollis. Mauris faucibus dignissim luctus. Aenean lacus nunc, ultricies ac ante eu, Fringilla accumsan nisi. Cras et nunc posuere, Mattis mi at, Rutrum Felis. Etiam volutpat neque sodales condimentum consectetur. '

Some people say that Python is not a useful string that includes three quotes? This can be written in multiple lines, such as:

s = "" "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam bibendum Purus at Nibh rutrum Dapibus. Aliquam quis sem sagittis, rutrum magna Quis, Gravida quam. Sed Porta ex nec Leo Elementum, a Efficitur neque ornare. Ut tristique nunc vitae ultrices molestie. "" "

But that would be a problem, that is, each line will be followed by a newline character, if my string has only one row? This is not the right thing to do.

I think the better way is as follows:

1. Add an escape character at the end of a row

s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\aliquam bibendum purus at Nibh rutrum dapibus.\aliquam quis  SEM sagittis, Rutrum magna Quis, Gravida quam.\sed Porta ex nec Leo Elementum, a efficitur neque ornare.\ut tristique nunc Vitae ultrices molestie. "

2. implicit conversion using parentheses

s = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")    Aliquam bibendum Purus at Nibh rutrum dapibus. "    " Aliquam quis sem sagittis, rutrum magna Quis, Gravida quam. "    Sed Porta ex nec Leo Elementum, a Efficitur neque ornare. "    " Ut tristique nunc vitae ultrices molestie. ") # Note: There is no comma at the end of line

Personally think that the first way, although it can solve the problem but too ugly, recommend the use of the second method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.