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

Panchanathan Suresh suresh.gm at gmail.com
Fri Jan 24 13:03:29 EST 2020


Thanks a lot Mark and Alex, really appreciate it.

I thought about this more and then this worked  return members[:-2]
(-2 because there is an extra space after , in members = members + users[x]
+ ", ")

I was all along trying -1 and was unsuccessful.

Thanks again.

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

> Thanks a lot Mark and Alex, really appreciate it.
>
> I thought about this more and then this worked  return members[:-2]
> (-2 because there is an extra space after , in members = members +
> users[x] + ", ")
>
> On Fri, Jan 24, 2020 at 9:50 AM Alex Kleider <akleider at sonic.net> wrote:
>
>> On 2020-01-24 05:12, Panchanathan Suresh 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,
>> >
>> > I want it to return Mike, Karen, Jake, Tasha (no comma after Tasha)
>> >
>> > Below is my code:
>> >
>> > *****
>> > def group_list(group, users):
>> >   members = ""
>> >   members1 = ""
>> >   for x in range(0,len(users)):
>> >       #print(users[x])
>> >       members = members + users[x] + ", "
>> >   return members[:-1]
>> >
>> >
>> > print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) #
>> > Should
>> > be "Marketing: Mike, Karen, Jake, Tasha"
>> >
>>
>> You might find the str join method useful;
>> more specifically:
>> >>> ', '.join(["Mike", "Karen", "Jake", "Tasha"])
>>
>


More information about the Tutor mailing list