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

Alex Kleider akleider at sonic.net
Fri Jan 24 12:50:06 EST 2020


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