Skip to content

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)))
Code language: Python (python)

You can confirm this worked as follows:

print(str_new) print(type(str_new)) # '1, 2, 3' # <class 'str'>
Code language: Python (python)

Option 2 – Using repr()

r_str = repr(se)
Code language: Python (python)

You can confirm this worked as follows:

print(r_str) print(type(r_str)) # {1, 2, 3} # <class 'str'>
Code language: Python (python)

See also  Custom RGB To Hex Conversion with Python
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x