Indentation under wrapped line

203 Views Asked by At

I'm beginning in programming and using Pycharm. I adopted 79 lines as maximous line length. But now I don't know if using an extra tab to indent the next line, since the previous line is already indented under the first. This shows what I mean:

I can use this:

if len(word) % 2 == 1:
    cent = len(word) // 2
    if (word[cent] == 'a' or word[cent] == 'e' or word[cent] == 'i'
            or word[cent] == 'o' or word[cent] == 'u'):
                print('The word's center is a lowercase vowel')

Or this:

if len(word) % 2 == 1:
    cent = len(word) // 2
    if (word[cent] == 'a' or word[cent] == 'e' or word[cent] == 'i'
            or word[cent] == 'o' or word[cent] == 'u'):
        print('The word's center is a lowercase vowel')

Either ways worked.

So, is there a convention for these situation. Thanks all in advance! Have a great day :)

2

There are 2 best solutions below

0
On

Per PEP8 https://www.python.org/dev/peps/pep-0008/#maximum-line-length:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

As for indentation on subsequent lines, More indentation included to distinguish this from the rest.:

https://www.python.org/dev/peps/pep-0008/#indentation

Code would look like this:

if len(word) % 2 == 1:
    cent = len(word) // 2
    if (word[cent] == 'a' or word[cent] == 'e' or word[cent] == 'i'
            or word[cent] == 'o' or word[cent] == 'u'):
        print("The word's center is a lowercase vowel")
0
On

You can use \ as last character in a line to signify "this line continues in the next line" - this helps where "normal" python code can not be broken (not your case).

Your example is better suited by

vowels = set("aeiou") # maybe faster then list-lookup if used several times
if word[cent] in vowels:

or by

if word[cent] in "aeiou":

or by

def isVowel(ch):
    return ch in "aeiou"

if isVowel(word[cent]):

PEP-8 maximum line lenght tells about how to "format correctly".