[Tutor] How to remove the comma only at the end?

Marc Tompkins marc.tompkins at gmail.com
Fri Jan 24 12:43:24 EST 2020


On Fri, Jan 24, 2020 at 9:21 AM Panchanathan Suresh <suresh.gm at gmail.com>
wrote:

> Hi everyone,
>
> How to remove the last comma , from the variable members?
> I tried members[:-1], members.rstrip(","), but no luck. It is
> always returning:
> Mike, Karen, Jake, Tasha,
>

Use the string.join() method:

>>> marketing = ["Mike", "Karen", "Jake", "Tasha"]
>>> ", ".join(marketing)
'Mike, Karen, Jake, Tasha'

Essentially, this says "Take the members of the list and glue them together
into a string, using this string (a comma and a space in this instance) as
glue".


More information about the Tutor mailing list