What exactly is the backslash ( ) for in Python?

Asked

Viewed 3,610 times

16

I sent an application to Github written in Python that was updated by someone who claimed to have formatted the code properly. After starting to see the changes, I got scared thinking that it had spoiled my program because this excerpt from my original code:

self.__bg_image = self.getPhotoImage(image_path=self.image_path,width=self.__width,height=self.__height,closeAfter=True)[0]

had turned into this:

self.__bg_image = \
self.getPhotoImage(image_path=self.image_path, width=self.__width, height=self.__height, closeAfter=True)[0]

So before complaining, I tested the updated code and to my surprise, it worked perfectly without any apparent change in the final result.

After that I was very curious to know exactly how this backslash works that I had never seen before. Can someone explain to me ?

  • Python version is 3.6 before anyone asks.

1 answer

21


\ "loose" in the code

The backslash outside of strings serves to indicate a line change without leaving the context of the current line (that is - after a \, you can continue the current line on the bass line normally)

resultado = variavel1 * variavel2 + \
    funcao3(variavel4) + variavel5  

In languages with C-derived syntax (Java, Javascript, PHP, C#, Objective C, C++, etc...) expressions can extend across multiple lines, and what defines the end of an expression is ;. In Python, line change plays the role of ; (which is optional in Python, but recommended only in the very rare times it is decided to put two commands on the same line)

On the other hand, the feeling for some Python programmers to be able to continue a line only with the \ is that things get a little "loose". e.g.: if you enter a blank line or comment there is a syntax error, and if this feature is not being used, it is always possible to leave a blank line at any point). So the preference, whenever possible, including the recommendation in style guides, is to put an extra pair of parentheses around expressions that get too big.

If the expression is within a pair of parentheses, it is allowed to break from line to will, until the ) - and it still marks the end of the exact expression - the feeling is that things get more tied up.

resultado = (variavel1 * variavel2 + 
    funcao3(variavel4) + variavel5  
)

The preference for parentheses over the years was so great that they even made changes in the syntax of the language to accept parentheses where it didn’t make sense before (for example, in the command import).

from projeto import (
    classe1, 
    classe2,
    classe3
)

Today, the only structure that still needs the \ to break the line in the case of very long lines is the command with with several context managers - you can’t group them with parentheses, because for Python’s Rhyme, this would be indistinguishable from a tuple.


with open(nome_arq1) as arq1, \
    open(nome_arq2) as arq2:

The \ can still be cute and is cool to use when choosing to share a very large string in several lines - but I would not use in other cases.

In this case there, and that see the intention of this "someone" who has updated his code - there is a practice of opening Prs with code formatting by which one deceives the statistics of code contribution in a project, or for his portfolio.

Shorter lines are cool - but the recommended way is to put large expressions in parentheses, not with the \.

And last but not least, in 2019 it became popular a code formatting tool that helps a lot with this - it adjusts the formatting of the code to the desired style recommendations, without the developers having to waste their time with it- is called "black" - https://github.com/psf/black - can be used by the command line, or integrated with an IDE.

In the case you put in the example of the question, in particular, instead of earning a few columns by breaking the row right after the variable name, the ideal is to put in several rows - taking advantage that already exist parentheses in the method call. That is instead of:

self.__bg_image = \
self.getPhotoImage(image_path=self.image_path, width=self.__width, height=self.__height, closeAfter=True)[0]

do:

self.__bg_image = self.getPhotoImage(
    image_path=self.image_path,
    width=self.__width,
    height=self.__height, 
    closeAfter=True
)[0]

The \ in strings

Inside any kind of string in Python - that is, in in the file ". py", text that comes between any type pair of quotation marks ', ", ''', """ and possibly combined one or more of the prefixes u, b, f (but not with the prefix r), the backslash has a completely different role.

In this case, Python also picked up a behavior from the C language - a \ inside strings serves as the Escape character - meaning it can be combined with the next (or next) characters to form a completely different character.

Python 3.8.0+ (heads/3.8:d04661f, Oct 24 2019, 09:19:45) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "c:\indios"
>>> b = "c:\ndios"
>>> "\\" in a
True
>>> "\\" in b
False
>>> print(a, b)
c:\indios c:
dios

That is - depending on what comes after the " "she cease to exist inside the string text, and turns into another code:

>>> a = "\t"
>>> b = "\i"
>>> len(a)
1
>>> len(b)
2
>>> ord(a[0])
9
>>> ord(b[0])
92

To ensure that it exists as " even, it is recommended to always use the form \\ (two bars) - two bars combine into a single character. In Windows, like the \ is used in cmd to separate directories this becomes much more dangerous - because a program may work or not depending on the first letter of a file name within a folder. (Only it is not worse because if the file name is in a variable, instead of written directly in a single string within the file. py, replacement does not happen)

Useful sequences with \ in strings:

  • \n - this is the most used, indicates a line break (NEWLINE special character, hexadecimal code 0x0a) - if used in a print, saved in a file, etc, makes the text change line and continue at the bottom line
  • \t TAB special character (Hex 0x09): A special spacer that makes the printing of the text advance until the next "tab column" (in general, multiples of 8, but is configurable) - very important when the computers had much less memory and it made a lot of difference have a single character with code 9, spending a single byte instead of up to 8 space characters - in text files containing tables, etc... Its use today can confuse many people, because the number of spaces it generates is variable - I only recommend the use by those who know exactly what you’re doing (and always configure the programming editor to use "spaces" instead of "tabs"):
>>> for i in range(15):
...     print(f"{'#' * i}\t#")
... 
        #
#       #
##      #
###     #
####    #
#####   #
######  #
####### #
########        #
#########       #
##########      #
###########     #
############    #
#############   #
##############  #
>>> 
  • \b BACKSPACE ( x08) this one is very interesting: it makes the print "back" a character backwards. Can be used in simple command line programs to print percentage progress, rewriting the value with print (sorry, I won’t make a movie, but the Python code below can be pasted into an interactive interpreter:
import time
for i in range(0, 100, 5):
    print("\b\b\b\b\b\b", i, "%",  sep="", end="", flush=True)
    time.sleep(0.1)
  • \r - "Carriage Return" - 0x0d - on current systems makes the print go back to the beginning of the current line, but without changing lines (similar to b)
  • \\ one \ normal - which guarantor works
>>> print("\\")
\
  • \" - bar can be used to place quotes of the same type used to open the string inside the string:
>>> a = "teste: \"alo\""                                                                                                                                                       

>>>  print(a)                                                                                                                                                                   
teste: "alo"
  • \ alone, at the end of a line, but within a string: it has the same effect as the \ used in code: Python ignores the line-change character after " " and makes the text continue without breaking. This works in any style of strings, but since the string will use multiple lines of the program, the ideal is to use strings with triple quotes, which can have multiple lines in the file. py:
>>> a = """ Vou contar uma história:\
...  em uma linha só"""
>>> print(a)
Vou contar uma história: em uma linha só
  • \xHH - allows printing of any Unicode character code up to 255, direct by its hexadecimal value.
>>> print("\xb1")                                                                                                                                                              
±
  • \uHHHH - like the \x but uses four hexa digits, allowing any Unicode character of the "BMP" (Plane Multilingual Base) - with more than 60000 characters possible. In addition to almost all writing systems in the world, in this track we also have some emojis
>>> print ("\u2665")                                                                                                                                                           
♥
  • \UHHHHHHHH - 'Uppercase U uses eight hexa digits, allowing any Unicode character at all (although the defined maximum fits in only 6 hexa digits)
print ("\U0001F600 \U0001F46A")                                                                                                                                            
 

(these emojis won’t work on the standard Windows CMD, but download the new application from "terminal" in windows store if you are on this operating system - on Mac and Linux they appear normally)

\ inside strings with prefix r -

The prefix "r" was created because sometimes the transformation of the \ is not desired, and may be inconvenient - in that case, almost all special combinations of \ are ignored, and it is always inserted as it was typed.

This is important to be able to type regular expressions, or other texts where " " is a significant character - for example - in names of files on Windows:

>>> a = "\xb1"
>>> b = r"\xb1"
>>> print(a, b)
± \xb1

In the case of regular expressions, the \ can mean many different things - in general it is used for "escape" too - but the regular expression engine has to know "what" comes after " - without the prefix r" Python translates the characters, as above, already in the compilation, and the regular expression engine would see only the codes already transformed.

  • Wow... you went much further than I was needing kkk. Thank you for the reply jsbueno <3

  • I liked the package, because it is already documented there - even in English it is difficult to find all this in one place. (The tip from \ at the end of a line within of a string, for example, is something that can confuse even experienced developers in the language)

  • Still missing special codecs that can interpret "" - but ran out of gas here. :-)

  • I just didn’t understand very well the part you talked about opening practices "Prs" etc. Can I explain better ?

  • PR = Pull Request - is how other people make modifications to projects they don’t have direct access to on github. Or how it was that the person changed his project there?

  • Ata, I didn’t know what the acronym "PR".

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.