The easiest and simplest way to convert a set to a string in Python is to use the str() function, which converts the set to its string representation, including the curly braces like “{item1, item2}”.
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}
print(my_set)
# Output: {'Vlad', 'Lisa', 'Isaac', 'Trevor'}
print(type(my_set))
# Output: <class 'set'>
# Converting the set to a string
set_string = str(my_set)
print(set_string)
# Output: {'Vlad', 'Lisa', 'Isaac', 'Trevor'}
print(type(set_string))
# Output: <class 'str'>
You can see that sets are unordered collections, so while the elements will all be present in the string, their order is not guaranteed to be the same every time the code is run.
Empty Set
The empty set returns the empty string because it contains no elements.
empty_set = set() print(empty_set) # Output: set() print(type(empty_set)) # Output: <class 'set'> print(str(empty_set)) # Output: set() print(type(str(empty_set))) # Output: <class 'str'>
Sets with duplicates
If the set contains duplicate elements, it automatically removes them, ensuring that the output string does not contain any duplicates.
dup_set = set([1, 2, 2, 3])
print(dup_set)
# Output: {1, 2, 3} (order may vary)
print(len(dup_set))
# Output: 3
print(type(dup_set))
# Output: <class 'set'>
converted_str = ', '.join(map(str, dup_set))
print(converted_str)
# Output: 1, 2, 3 (order may vary)
print(len(converted_str))
# Output: 7 (Including commas and spaces)
print(type(converted_str))
# Output: <class 'str'>
Here, from the output, you can see that the set contains only three elements and it has a type of <class ‘set’>.
However, when you convert it to a string, its data type changes to <class ‘str’>.
However, if you find the length of the string, it is now seven, taking into account the spaces and commas.
When you count the number of elements in a string, it counts characters, commas, and white spaces as separate characters.
Nested structures
The set is not hashable, but if it contains tuples, they are hashable. So, if a set contains tuples, it will properly convert to a string using the str() function.
valid_nested_set = {(1, 2), (3, 4)}
print(valid_nested_set)
# Output: {(1, 2), (3, 4)}
print(type(valid_nested_set))
# Output: <class 'set'>
print(str(valid_nested_set))
# Output: {(1, 2), (3, 4)}
print(type(str(valid_nested_set)))
# Output: <class 'str'>
Alternate approaches
Using string.join() method
The string.join() method concatenates the elements of the set into a single string, separated by a specified delimiter (like commas or spaces).
The main advantage of the join() method is that the output string does not include the curly braces “{}”. It returns a clean string that you can use for further processing.
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}
print(my_set)
# Output: {'Vlad', 'Trevor', 'Lisa', 'Isaac'}
print(type(my_set))
# Output: <class 'set'>
set_string = ', '.join(my_set)
print(set_string)
# Output: Vlad, Trevor, Lisa, Isaac
print(type(set_string))
# Output: <class 'str'>
Using functools.reduce() function
The functools.reduce() function takes two arguments: a lambda function and a set. The lambda function concatenates the current accumulated string (acc) with the next item in the set, each separated by a comma and a space.
from functools import reduce
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}
print(my_set)
# Output: {'Trevor', 'Vlad', 'Isaac', 'Lisa'}
print(type(my_set))
# Output: <class 'set'>
set_string = reduce(lambda acc, item: acc + ', ' + item, my_set)
print(set_string)
# Output: Trevor, Vlad, Isaac, Lisa
print(type(set_string))
# Output: <class 'str'>
Using List Comprehension
A list of strings can be created using list comprehension and then concatenated using the join() method.
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}
print(my_set)
# Output: {'Vlad', 'Isaac', 'Trevor', 'Lisa'}
print(type(my_set))
# Output: <class 'set'>
set_string = ', '.join([str(item) for item in my_set])
print(set_string)
# Output: Vlad, Isaac, Trevor, Lisa
print(type(set_string))
# Output: <class 'str'>
That’s all!



