Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Set to String in Python

  • 13 Oct, 2025
  • Com 0
How to Convert a Set to String in Python

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}”.

Converting a Set to String in Python

 

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.Using join() method

 

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.

Using functools.reduce() method

 

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!

Post Views: 22
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Float (Double) to String in Python
How to Convert Image to Base64 String and Vice Versa in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend