Python: how to join entries in a set into one string?

前端 未结 8 1212
青春惊慌失措
青春惊慌失措 2020-12-13 17:06

Basically, I am trying to join together the entries in a set in order to output one string. I am trying to use syntax similar to the join function for lists. Here is my atte

8条回答
  •  不知归路
    2020-12-13 17:19

    Sets don't have a join method but you can use str.join instead.

    ', '.join(set_3)
    

    The str.join method will work on any iterable object including lists and sets.

    Note: be careful about using this on sets containing integers; you will need to convert the integers to strings before the call to join. For example

    set_4 = {1, 2}
    ', '.join(str(s) for s in set_4)
    

提交回复
热议问题