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) continueSee 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.
Summary: in this tutorial, you’ll learn about the Python backslash character as a part of a special sequence character or to escape characters in a string.
Introduction to the Python backslash
In Python, the backslash(\) is a special character. If you use the backslash in front of another character, it changes the meaning of that character.
For example, the t is a literal character. But if you use the backslash character in front of the letter t, it’ll become the tab character (\t).
Generally, the backslash has two main purposes.
First, the backslash character is a part of special character sequences such as the tab character \t or the new line character \n.
The following example prints a string that has a newline character:
print('Hello,\n World')
Code language: PHP (php)Output:
Hello, World
The \n is a single character, not two. For example:
s = '\n' print(len(s)) # 1
Code language: PHP (php)Second, the backslash (\) escape other special characters. For example, if you have a string that has a single quote inside a single-quoted string like the following string, you need to use the backslash to escape the single quote character:
s = '"Python\'s awesome" She said' print(s)
Code language: PHP (php)Output:
"Python's awesome" She said
Code language: JavaScript (javascript)Backslash in f-strings
PEP-498 specifies that an f-string cannot contain a backslash character as a part of the expression inside the curly braces {}.
The following example will result in an error:
colors = ['red','green','blue'] s = f'The RGB colors are:\n {'\n'.join(colors)}' print(s)
Code language: PHP (php)Error:
SyntaxError: f-string expression part cannot include a backslash
Code language: JavaScript (javascript)To fix this, you need to join the strings in the colors list before placing them in the curly braces:
colors = ['red','green','blue'] rgb = '\n'.join(colors) s = f"The RGB colors are:\n{rgb}" print(s)
Code language: PHP (php)Output:
The RGB colors are: red green blue
Backslash in raw strings
Raw strings treat the backslash character (\) as a literal character. The following example treats the backslash character \ as a literal character, not a special character:
s = r'\n' print(s)
Code language: PHP (php)Output:
\n
Summary
- The python backslash character (\) is a special character used as a part of a special sequence such as \t and \n.
- Use the Python backslash (\) to escape other special characters in a string.
- F-strings cannot contain the backslash a part of expression inside the curly braces {}.
- Raw strings treat the backslash (\) as a literal character.
Did you find this tutorial helpful ?