pythonstringline-continuation

In python, how can I remove the last character in a print that is also using line continuation? Tried rstrip()


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]]


Solution

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

    print(", ".join("[[image:%s]]" % x for x in attendees_at))