How to convert Set to String in Python? In Python, you can convert a set to a string using various methods. One common approach is to use the str() function or the join() method. A Set is a one-dimensional data structure that will hold unique elements. It can be possible to have the same or different type of elements in the set. Whereas the String is a sequence of characters. In this article, I will explain how to convert a set to a string in Python.
1. Quick Examples of Converting Set to String
If you are in a hurry, below are some quick examples of how to convert a set to a string in Python.
# Quick examples of converting set to string
# Convert myset to String using join()
converted = ', '.join(myset)
print("Converted String:",converted)
# Convert myset to String using map()
converted = ', '.join(list(map(str,myset)))
print("Converted String:",converted)
# Convert myset to String using str()
converted = str(myset)
print("Final datatype: " , type(converted))
2. Convert Set to String using join()
To convert a Python set to a string you can use the join() method of the string class. This is a built-in function that takes the Python sets as an argument and joins each element of the set with a separator.
2.1 Syntax of join()
The following is the syntax of the join() function.
# Syntax
', '.join(myset)
Here, myset is an input set you wanted to convert to a string.
2.2 Convert Set to String Example
Let’s create a set with string values and use the join() function to convert the set to string type. In the below example I am using a space as a separator hence the resulting string will have values with a space separator.
# Create Set with Strings
myset = {"welcome","to","sparkby","examples"}
print("Original Set:",myset)
print("Previous datatype:",type(myset))
# Convert Set to String
converted = ' '.join(myset)
print("Converted set to string:",converted)
print("Final datatype:", type(converted))
Yields below output.

Let’s try another example with a comma separator.
# Convert set to string by comma separator
converted = ', '.join(myset)
print("Converted String:",converted)
# Output:
# Converted String: sparkby, to, welcome, examples
Similarly, you can also use the map() function with join() to convert the set to strings to a single string in Python. The map(str, myset) is used to convert each element of the set to a string, and then ','.join() is used to join them with a comma and space separator.
# Using map() with join()
result = ', '.join(map(str, myset))
print(result)
# Output:
# sparkby, welcome, examples, to
3. Using List Comprehension with join()
Alternatively, you can use list comprehension to create a list of strings from the set elements, and then use the join() method to combine them into a single string.
# Using list comprehension
result = ', '.join([str(elem) for elem in myset])
print(result)
4. Convert set to string using repr()
In Python, repr() or _repr__() is used to return the object representation in string format. When the repr() a function is invoked on the object, __repr__() method is called.
4.1 repr() Syntax
# Syntax
repr(myset)
4.2 Using repr() Function
If you want to use repr() to convert a set to a string, you can apply it to the entire set. The repr() function is used to obtain the official string representation of an object, and it includes the curly braces {} to denote a set in this case.
# Create set with 4 strings
myset = {"welcome","to","sparkby","examples"}
print("Set: ",myset)
print("Previous datatype: ",type(myset))
# Convert myset to String using repr()
converted = repr(myset)
print("Final datatype: " , type(converted))
# Output:
# Set: {'sparkby', 'to', 'welcome', 'examples'}
# Previous datatype: <class 'set'>
# Final datatype: <class 'str'>
5. Convert Set to String using str()
To convert the Set to String you can use the str() function in Python. The str() function is a built-in function that returns a string version of a given object. It converts any value or object to a string representation.
5.1 Syntax of str()
Following is the syntax of the str()
# Syntax
str(myset)
5.2 Using str() Convert Set to String Example
If you want to use str() to convert a set to a string, you can directly pass the set to the str() function. The str() function provides a string representation of the set, and in this case, it includes the curly braces {} to denote a set.
# Create Set with 4 strings
myset = {"welcome","to","sparkby","examples"}
print("Original Set:",myset)
print("Previous datatype: ",type(myset))
# Convert myset to String using str()
converted = str(myset)
print("Final datatype:",type(converted))
print("Converting set to string:",converted)
# Output:
# Original Set: {'examples', 'to', 'welcome', 'sparkby'}
# Previous datatype: <class 'set'>
# Final datatype: <class 'str'>
# Converting set to string: {'examples', 'to', 'welcome', 'sparkby'}
Frequently Asked Questions on Convert Set to String in Python
You can convert a set of integers to a string in Python using the join() method along with the str() function to convert each integer to a string.
You can convert a set of mixed data types to a string in Python. You can use the join() method along with the str() function to convert each element to a string.
To remove curly braces from the string representation of a set, you can use the join() method to concatenate the elements with a custom separator. For example, map(str, myset) converts each element in the set to a string, and ', '.join() concatenates these strings with a comma and space separator.
You can specify a different separator when converting a set to a string using the join() method. The separator is the string that will be used to concatenate the elements of the set.
There is a difference. str() directly converts an object to its string representation, which may include formatting specific to the object type (e.g., curly braces for sets). join() is used to concatenate strings with a specified separator, giving you more control over the format.
Conclusion
In this article, you have learned how to convert the given set to a string by using join(), map() and str() functions. In all examples, we verified the type after converting it to string.
Related Articles
- Python String in operator with Examples
- Python String reverse with Examples
- Python String length with Examples
- Python String append with Examples
- Python String Contains
- Python String find() with Examples
- Convert String to Uppercase in Python
- How to initialize Set in Python?
- How to subtract two sets in Python?
- Python set clear() method
- How to convert Python list to set?
- Python set comprehension examples