How to convert Set to String in Python? There are several ways to convert values from a set to a string. 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.
1. Quick Examples of Converting Set to String
Following are quick examples of how to convert a set to a string in python.
# Quick Examples
# 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 join() Syntax
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("Set: ",myset)
print("Previous datatype: ",type(myset))
# Convert Set to String
converted = ' '.join(myset)
print("Converted String:",converted)
print("Final datatype: " , type(converted))
# Output:
# Set: {'sparkby', 'to', 'welcome', 'examples'}
# Previous datatype: <class 'set'>
# Converted String: sparkby to welcome examples
# Final datatype: <class 'str'>
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
You can also use the map() function with join() to convert the set to strings to a single string in Python.
# Using map() with join()
result = ', '.join(map(str, myset))
print(result)
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() function is invoked on the object, __repr__() method is called.
4.1 repr() Syntax
# Syntax
repr(myset)
4.2 Example
# 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 str() Syntax
Following is the syntax of the str()
# Syntax
str(myset)
5.2 Using str() Convert Set to String Example
Let’s create a Set with string and use it with the str() to convert it to string type.
# Create Set with 4 strings
myset = {"welcome","to","sparkby","examples"}
print("Set: ",myset)
print("Previous datatype: ",type(myset))
# Convert myset to String using str()
converted = str(myset)
print("Final datatype: " , type(converted))
print("Result: ", converted)
# Output:
# Set: {'to', 'sparkby', 'examples', 'welcome'}
# Previous datatype: <class 'set'="">
# Final datatype: <class 'str'="">
Result: {'sparkby', 'to', 'welcome', 'examples'}
</class></class>
7. 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