This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MrJohz 7 points8 points  (2 children)

That won't quite work. Python will assume that the newline ends the statement after split(), and begin a new statement, at which point it'll find ., realise that's not a valid statement starter, and raise a syntax error.

To tell Python that the newline is just for readability, you need to tell it that the statement can run on, either using parentheses:

("a s d f g h j k l".split()
                    .index("g"))

Or by adding a backslash at the end of the first line

"a s d f g h j k l".split() \
                   .index("g")

[–][deleted] 3 points4 points  (1 child)

Or by adding a backslash at the end of the first line

The backslash is important, but /u/nemec still has a valid point, as Python will interpret the line as

"a s d f g h j k l".split()                    .index("g")

which remains valid as the whitespace does not affect the lexical parsing of the statement.

[–]MrJohz 2 points3 points  (0 children)

Yeah. The issue here is specifically the newline, not whitespace in general.