print function help percentage and slash (multiple variables)
 print function help percentage and slash (multiple variables) leodavinci1990 Silly Frenchman Posts: 31 Threads: 22 Joined: Feb 2019 Reputation: Aug-09-2020, 10:36 PM (This post was last modified: Aug-09-2020, 10:36 PM by leodavinci1990.) 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 bowlofred Da Bishop Posts: 1,258 Threads: 3 Joined: Mar 2020 Reputation: Aug-09-2020, 10:53 PM (This post was last modified: Aug-10-2020, 09:25 PM by bowlofred.) 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 "", 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 leodavinci1990 Silly Frenchman Posts: 31 Threads: 22 Joined: Feb 2019 Reputation: Aug-10-2020, 02:21 AM (This post was last modified: Aug-10-2020, 02:21 AM by leodavinci1990.) 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 bowlofred Da Bishop Posts: 1,258 Threads: 3 Joined: Mar 2020 Reputation: Aug-10-2020, 02:51 AM "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 output correction using print() function afefDXCTN 3 3,122 Sep-18-2021, 06:57 PM Last Post: Sky_Mx Function - Return multiple values tester_V 10 1,062 Jun-02-2021, 05:34 AM Last Post: tester_V print function output wrong with strings. mposwal 5 826 Feb-12-2021, 09:04 AM Last Post: DPaul How to print multiple elements from multiple lists in a FOR loop? Gilush 6 760 Dec-02-2020, 07:50 AM Last Post: Gilush Output with none, print(x) in function Vidar567 3 609 Nov-24-2020, 05:40 PM Last Post: deanhystad How to print string multiple times on new line ace19887 7 1,681 Sep-30-2020, 02:53 PM Last Post: buran Do I have to pass 85 variables to function? Milfredo 10 1,339 Sep-26-2020, 10:13 PM Last Post: Milfredo Print variable values from a list of variables xnightwingx 3 815 Sep-01-2020, 02:56 PM Last Post: deanhystad Deleting multiple variables/arrays Robotguy 0 510 Aug-18-2020, 09:56 PM Last Post: Robotguy Help Setting Multiple Variables bzowk 0 603 Jul-18-2020, 06:59 PM Last Post: bzowk

Forum Jump:

User Panel Messages

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