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)
#1
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
p1=(10,10)
p2=(10,20)
p3=(20,20)

print("The distance between %s and %s is %0.1f."%\
(str(p1),str(p2),distance(p1,p2)))
print("The distance between %s and %s is %0.1f."%\
(str(p1),str(p3),distance(p1,p3)))
Reply
#2
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
5
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 """
Reply
#3
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"?
Reply
#4
"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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  How to print variables in function? samuelbachorik 3 852 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  change backslash into slash in a path paul18fr 7 2,179 Jul-21-2022, 04:26 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,592 May-25-2022, 07:22 PM
Last Post: Gribouillis
Sad Iterate randint() multiple times when calling a function Jake123 2 1,981 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,696 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  How can I assign "multiple variables" to a single "value"? Psycpus 2 1,810 Oct-04-2021, 03:29 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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