How to Convert Set to String in Python

Here are 4 ways to convert a set to a string in Python:

  1. Using str() function
  2. Using join() method
  3. Using functools.reduce() method
  4. Using List Comprehension

Method 1: Using str() function

The easiest way is to use the str()  function, which converts the set to its string representation, including the curly braces.

Visual Representation

Python Convert Set to String using str() method

Example

# Define a set of string elements
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}

# Print the original set and its type
print("=== Before Conversion ===")
print(my_set) 
print(type(my_set)) 

# Convert the set to a string
set_string = str(my_set)

# Print the string representation of the set and its type
print("=== After Conversion ===")
print(set_string) 
print(type(set_string)) 

Output

=== Before Conversion ===
{'Isaac', 'Trevor', 'Vlad', 'Lisa'}
<class 'set'>
=== After Conversion ===
{'Isaac', 'Trevor', 'Vlad', 'Lisa'}
<class 'str'>

Note 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.

Method 2: Using 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).

Visual Representation

Using join() method

Example

# Define a set of string elements
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}

print("=== Before Conversion ===")
print(my_set)
print(type(my_set))

# Elements of the set are joined by ', ' and converted into a single string
set_string = ', '.join(my_set)

print("=== After Conversion ===")
print(set_string)
print(type(set_string))

Output

=== Before Conversion ===
{'Vlad', 'Isaac', 'Lisa', 'Trevor'}
<class 'set'>
=== After Conversion ===
Vlad, Isaac, Lisa, Trevor
<class 'str'>

Method 3: Using functools.reduce() function

The functools.reduce() function takes two arguments: a lambda function and the 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.

Visual Representation

Using functools.reduce() function

Example

from functools import reduce

# Define a set of string elements
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}

print("=== Before Conversion ===")
print(my_set)
print(type(my_set))

#Using functools.reduce() with a lambda function
set_string = reduce(lambda acc, item: acc + ', ' + item, my_set)

print("=== After Conversion ===")
print(set_string)
print(type(set_string))

Output

{'Trevor', 'Isaac', 'Vlad', 'Lisa'}
<class 'set'>
=== After Conversion ===
Trevor, Isaac, Vlad, Lisa
<class 'str'>

Method 4: Using List Comprehension

A list of strings can be created using list comprehension and then concatenated using the join() method.

Example

# Define a set of string elements
my_set = {'Vlad', 'Trevor', 'Isaac', 'Lisa'}

print("=== Before Conversion ===")
print(my_set)
print(type(my_set))

set_string = ', '.join([str(item) for item in my_set])

print("=== After Conversion ===")
print(set_string)
print(type(set_string))

Output

=== Before Conversion ===
{'Isaac', 'Trevor', 'Vlad', 'Lisa'}
<class 'set'>
=== After Conversion ===
Isaac, Trevor, Vlad, Lisa
<class 'str'>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.