-2

I am currently using Python 3.9.6 version and while using the print() statement with "sep=" as an argument, i am getting the syntax error.

My code for "Sep" Argument is: print("John","Sam","Michael",sep="-")

My code for "End" Argument is: print("John","Sam","Michael",end="-")

I don't understand what's the problem with my code. Please help

Please find the attached snippet of both the codes:enter image description here

enter image description here

Thanks in advance!!

  • 1
    Please provide the entire traceback and relevant code. There is nothing wrong with the `print` statement, so the syntax error must be coming from somewhere else – Wondercricket Jun 20 '22 at 15:49
  • 1
    There is no `print` in those screenshots. – user2357112 Jun 20 '22 at 16:17
  • Actually I am using IDLE's interactive mode and for it to work you don't need to use print statement. You can only get the output by just hitting enter. – Geetu Sethi Jun 20 '22 at 16:24
  • There is a difference between using `print` to write to standard output and letting the interpreter output the result of an *expression*. You are trying to assign a tuple to a name with a malformed expression. – chepner Jun 24 '22 at 18:38
  • `print(x, sep="-")` is not just a keyword followed by a tuple; it's a function call, the syntax of which allows keyword arguments. Expression lists do *not*. – chepner Jun 24 '22 at 18:40
  • This would have been obvious to everyone had you actually reproduced the code in your screen shot as text, rather than misleading everyone with code that does *not* produce a syntax error. – chepner Jun 24 '22 at 18:41

2 Answers2

0

try this :

print("John","Sam","Michael",sep,"=","-")
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
ana55e
  • 1
  • 1
0

Try using end.

Ex: print("John","Sam","Michael",end="-")

  • I just want to separate th output with hyphen in it like this: John-Sam-Michael – Geetu Sethi Jun 20 '22 at 15:59
  • Still getting the same error – Geetu Sethi Jun 20 '22 at 16:03
  • Can you please check the edited code – Geetu Sethi Jun 20 '22 at 16:06
  • It's weird because `print("John","Sam","Michael",sep="-")` works can you post the output image of `print("John","Sam","Michael",sep="-")` statement. From you image you can even do something like this `a="-".join(('10','20','30'))` and then print(a) note this only works for set of strings – Raghuram Sadineni Jun 20 '22 at 16:06
  • I have posted the new image please check – Geetu Sethi Jun 20 '22 at 16:09
  • This a="-".join(('10','20','30')) code worked, thanks!!! – Geetu Sethi Jun 20 '22 at 16:12
  • Would you be able to figure out why my code wasn't working – Geetu Sethi Jun 20 '22 at 16:16
  • Well, In the statement `a=(10,20,30,sep='-')` you are trying to assign a tuple to variable `a`. A tuple doesn't have the attribute `sep` so it does not work. Whereas in the solution `a="-".join(('10','20','30'))` you are creating a tuple, converting it into string by joining elements using `-`. `print("John","Sam","Michael",sep="-")` also works but you are not printing the result. – Raghuram Sadineni Jun 20 '22 at 16:47
  • Yes, you are right, this time I just written the print("John","Sam","Michael",sep="-") statement only without storing this into any variable and it worked. Thanks for helping me in understanding this issue. – Geetu Sethi Jun 20 '22 at 17:38