How to Convert a Set to a String in Python


If you need to convert a set to a string in Python, then you can do one of the following:

Option 1 – Using map() and join()

str_new = ', '.join(list(map(str, se)))

You can confirm this worked as follows:

print(str_new)
print(type(str_new))

# '1, 2, 3'
# <class 'str'>

Option 2 – Using repr()

r_str = repr(se)

You can confirm this worked as follows:

print(r_str)
print(type(r_str))

# {1, 2, 3}
# <class 'str'>