Convert a Set to a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Convert a Set to a String in Python #

Use the str.join() method to convert a set to a string, e.g. result = ','.join(str(item) for item in set). The str.join() method will join the set's elements into a string based on the provided separator.

main.py
my_set = {4, 5, 6} # ✅ convert set to str result = str(my_set) print(result) # 👉️ '{4, 5, 6}' print(type(result)) # 👉️ <class 'str'> # ----------------------------------------- # ✅ convert set of strings to string set_of_str = {'apple', 'banana', 'kiwi'} result = ','.join(set_of_str) print(result) # 👉️ 'banana,kiwi,apple' # ----------------------------------------- # ✅ convert set of integers to string set_of_int = {4, 5, 6} result = ','.join(str(item) for item in set_of_int) print(result) # 👉️ '4,5,6'

The first example in the code snippet uses the str() class to convert a set to a string.

main.py
my_set = {4, 5, 6} # ✅ convert set to str result = str(my_set) print(result) # 👉️ '{4, 5, 6}' print(type(result)) # 👉️ <class 'str'>

The str() class converts the set object to a string, keeping the curly braces.

You can use the join() method to convert a set to a string without the curly braces.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your set contains numbers or other types, convert all of the values to string before calling join().

main.py
set_of_int = {4, 5, 6} result = ','.join(str(item) for item in set_of_int) print(result) # 👉️ '4,5,6'

The example uses a generator expression to convert each integer in the set to a string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we convert the current set element to a string and return the result.

The string the join() method is called on is used as the separator between the elements.

main.py
set_of_int = {4, 5, 6} result = '-'.join(str(item) for item in set_of_int) print(result) # 👉️ '4-5-6'

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
set_of_int = {4, 5, 6} result = ''.join(str(item) for item in set_of_int) print(result) # 👉️ '456'

If you need to print the set's elements without brackets and separated by spaces, call the str.join() method on a string containing a space.

main.py
set_of_int = {4, 5, 6} result = ' '.join(str(item) for item in set_of_int) print(result) # 👉️ '4 5 6'

If you need to convert a list of set objects to a string, use 2 calls to the str.join() method.

main.py
list_of_sets = [{1, 2}, {3, 4}, {5, 6}] result = ','.join(','.join(str(element) for element in s) for s in list_of_sets) print(result) # 👉️ '1,2,3,4,5,6'

The inner call to the join() method joins the items of the set of the current iteration.

We used the str() class to convert each number to a string.

The last step is to use the join() method to join the set objects in the list into a string with a comma separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.