TOO
TOO

Reputation: 11

Python: What does backslash and a positive sign mean?

I am going thorough a code, I have seen different operators before, but "+ \" is a little but strange. This is the line of the code:

self.spam_words + \

Does anyone know what this operator "+ \" means in python? i have c++ background Th

Upvotes: 0

Views: 22

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51643

\ if not followed by anything else tells the interpreter that the line does not end here, and it glues the next line to this one.

Its probably just to follow PEP 008 style guide 79 char limit and format stuff nicely.

test = "some" + \
"text"

print(test)

Output:

sometext

See https://www.python.org/dev/peps/pep-0008/#id19 and look for line continuation:

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.

The pep also tells you to break where it is possible w/o resorting to \, f.e.:

test = ["sometext_{}".format(a) # does not need a \
        for a in range (200)]

Upvotes: 1

Related Questions