Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a backslash by itself ('\') mean in Python? [duplicate]

Tags:

python

I found this code in the nltk documentation (http://www.nltk.org/_modules/nltk/sentiment/vader.html)

if (i < len(words_and_emoticons) - 1 and item.lower() == "kind" and \
            words_and_emoticons[i+1].lower() == "of") or \
            item.lower() in BOOSTER_DICT:
            sentiments.append(valence)
            continue

Can someone explain what this if condition means?

like image 224
Nikhil Prabhu Avatar asked Jun 30 '16 13:06

Nikhil Prabhu


People also ask

What does a single backslash mean in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

What does double back slash mean in Python?

The double slash (//) operator is used in python for different purposes. One use of this operator is to get the division result. The division result of two numbers can be an integer or a floating-point number.

What does a slash mean in Python?

In Python, you use the double slash // operator to perform floor division. This // operator divides the first number by the second number and rounds the result down to the nearest integer (or whole number).

How do you fix a backslash as a string in Python?

In short, to match a literal backslash, one has to write '\\\\' as the RE string, because the regular expression must be "\\", and each backslash must be expressed as "\\" inside a regular Python string literal.


3 Answers

A backslash at the end of a line tells Python to extend the current logical line over across to the next physical line. See the Line Structure section of the Python reference documentation:

2.1.5. Explicit line joining

Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:

if 1900 < year < 2100 and 1 <= month <= 12 \
   and 1 <= day <= 31 and 0 <= hour < 24 \
   and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
        return 1

There is also the option to use implicit line joining, by using parentheses or brackets or curly braces; Python will not end the logical line until it finds the matching closing bracket or brace for each opening bracket or brace. This is the recommended code style, the sample you found should really be written as:

if ((i < len(words_and_emoticons) - 1 and item.lower() == "kind" and
        words_and_emoticons[i+1].lower() == "of") or
        item.lower() in BOOSTER_DICT):
    sentiments.append(valence)
    continue

See the Python Style Guide (PEP 8) (but note the exception; some Python statements don't support (...) parenthesising so backslashes are acceptable there).

Note that Python is not the only programming language using backslashes for line continuation; bash, C and C++ preprocessor syntax, Falcon, Mathematica and Ruby also use this syntax to extend lines; see Wikipedia.

like image 127
Martijn Pieters Avatar answered Oct 20 '22 19:10

Martijn Pieters


In this case, the \ is escaping the following new line character. Because Python cares about whitespace, this code is using this to allow code to be continued on a new line.

like image 3
DKing Avatar answered Oct 20 '22 19:10

DKing


It is used as a line break so the if condition can be written in the next line.

like image 2
DeepSpace Avatar answered Oct 20 '22 18:10

DeepSpace