Convert Set to String in Python
Last Updated :
07 Mar, 2025
Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}" etc.
Let's discuss some of the methods of doing it with examples.
Using str()
The simplest and most direct method to convert a set into a string is using str(). It retains the curly braces {} and represents the set in a human-readable format.
Python
a = {1, 2, 3}
res = str(a) # direct conversion
print(type(res), res)
Output<class 'str'> {1, 2, 3}
Using f-string
f-strings (f"{a}") offer a simple and readable way to convert a set to a string. They directly format the set as a string, maintaining its curly braces. This method is useful when quick and concise formatting is needed.
Python
a = {1, 2, 3}
res = f"{a}"
print(type(res), res)
Output<class 'str'> {1, 2, 3}
Using repr()
repr() function provides an official string representation of the set, including curly braces {}. This method is best suited for debugging and logging purposes since it ensures an exact representation of the set.
Python
a = {1, 2, 3}
res = repr(a) # Returns official string representation
print(type(res), res)
Output<class 'str'> {1, 2, 3}
Using join()
join() method is ideal when we want to convert a set into a clean, formatted string without curly braces {}. Since join() only works with strings, we first use map(str, a) to convert all elements to strings before joining them.
Python
a = {1, 2, 3}
res = ", ".join(map(str, a))
print(type(res), res)
Output<class 'str'> 1, 2, 3
Explanation:
- map(str, s) converts each element in the set to a string.
- join() merges all the converted strings into a single string, separated by ','.
Using json.dumps()
json.dumps() is a useful approach when we need a JSON-style string representation of a set. Since sets are not JSON serializable, we first convert the set into a list.
Python
import json
a = {1, 2, 3}
res = json.dumps(list(a)) # Convert set to list, then JSON string
print(type(res), res)
Output<class 'str'> [1, 2, 3]
Explanation:
- list(a) converts the set a to a list
- json.dumps() formats it as a string similar to a JSON array.
Similar Reads
Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr
1 min read
Convert Hex to String in Python Hexadecimal (base-16) is a compact way of representing binary data using digits 0-9 and letters A-F. It's commonly used in encoding, networking, cryptography and low-level programming. In Python, converting hex to string is straightforward and useful for processing encoded data.Using List Comprehens
2 min read
Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver
3 min read
Convert String to Long in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting a string to long. Converting String to long A long is an integer type value that has unlimited length. By converting a string into long we are transl
1 min read
Convert Object to String in Python Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming.Since every element in Python is an object, we can use the built-in str() and repr() m
2 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read