-2

I'm having a difficult time removing the final character in my line continuation print loop. Not sure if rstrip() is the correct solution or not.

It's my understanding that a ',' included at the end of a print statement i.e., 'print(),' will cause line continuation instead of creating a newline for each loop. The line continuation is desired, however I want to remove the last ',' from the print output.

I've tried various forms of print().rstrip(','), however it has either removed all of the ',' or resulted in a syntax error. Neither of which are desired.

for attendee in attendees_at: print('[[image:%s]],' %(attendee),

Current output:

[[image:aaa]], [[image:bbb]], [[image:ccc]],

Undesired output, all of the trailing ',' have been removed:

[[image:aaa]] [[image:bbb]] [[image:ccc]]

Desired output is the same as the current output, however the last ',' is removed:

[[image:aaa]], [[image:bbb]], [[image:ccc]]

  • Add `print'\b '`. <- that's a dirty dirty hack – inspectorG4dget Jul 21 '19 at 23:47
  • 1
    @inspectorG4dget: It also only works under very limited conditions. It's the kind of thing that seems to work when you're trying it out interactively, only to completely fail when it goes into an autograder or a pipeline or any sort of processing that will see the actual backspace character. – user2357112 Jul 21 '19 at 23:51

1 Answers1

1

Generally the easiest approach is to join the string before printing it:

print(", ".join("[[image:%s]]" % x for x in attendees_at))
donkopotamus
  • 22,114
  • 2
  • 48
  • 60