Python Forum
print function help percentage and slash (multiple variables)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print function help percentage and slash (multiple variables)
What is the function of adding a back slash in the following code after the percentage operator? I am testing it without the slash and it gives the same result.
Please note that I know I am not currently using the best practice for string formatting (format function method) in this example.

def distance(a,b):
    """ a and b are tuples. Finds the distance between them """
    return ((a[0]-b[0])**2+(a[1]-b[1])**2)**0.5

print("The distance between %s and %s is %0.1f."%\
print("The distance between %s and %s is %0.1f."%\
The author of this code wanted to split the print across multiple lines. Most python statements need to be on a single line. But there are cases where it can continue on to another line.

One method of splitting a line is to use a terminal backslash. If I try to put a return within a regular statement without the backslash, it will complain
>>> a = 2 +
  File "<stdin>", line 1
    a = 2 +
SyntaxError: invalid syntax
>>> a = 2 + \
... 3
>>> a
Because this works and because this method is how many other languages like C allow for line splitting, folks may get in the habit of always using the backslash when splitting statements across lines. But there are some other situations where line splitting is allowed without backslashes: triple quotes, and parenthesis/bracket/braces.

Your code above is within parenthesis, so even without the backslash, the return does not terminate the statement.

>>> a = (2 +
... 3)
>>> a = {2 +
... 3}
>>> a = [2,
... 3]
>>> a = """ A two and
... a three """
Hi. Thanks bowlfred. That was super-helpful. As I tried to search for an explanation online and this answered it.
Can I have one question. What did you mean by return in

"the return does not terminate the statement"?
"return" as in carriage return. When you hit return to go to the next line, it terminates the statement unless you are inside parentheses/brackets/braces, you are in a triple-quote, or the return is escaped with a preceding backslash.

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I assign "multiple variables" to a single "value"? Psycpus 2 206 Oct-04-2021, 03:29 AM
Last Post: deanhystad
  output correction using print() function afefDXCTN 3 3,584 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  Function - Return multiple values tester_V 10 1,251 Jun-02-2021, 05:34 AM
Last Post: tester_V
  print function output wrong with strings. mposwal 5 958 Feb-12-2021, 09:04 AM
Last Post: DPaul
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 857 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Output with none, print(x) in function Vidar567 3 731 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  How to print string multiple times on new line ace19887 7 1,984 Sep-30-2020, 02:53 PM
Last Post: buran
  Do I have to pass 85 variables to function? Milfredo 10 1,524 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Print variable values from a list of variables xnightwingx 3 930 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Deleting multiple variables/arrays Robotguy 0 578 Aug-18-2020, 09:56 PM
Last Post: Robotguy

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020