In this Python tutorial, we’ll learn how to properly convert Python set to string using 5 different methods. We’ll understand these methods with the help of practical Python code examples.
Introduction: Python Set To String
Python set to used to store unordered list of items. In our following examples, we’ll see how to change the type of Python set from set to string and also we’ll learn how to create a new string using the items of existing set.
Let’s now understand these methods using practical Python code examples.
Implementing Python Set To String (5 Methods)
These 5 methods are listed below:
- Using str() Function
- Using join() Function
- Using repr() Function
- Using map() Function
- Using Python for loop
Using str() Function
This function is used to convert the type of object to string. For demonstration, we’ll first create a Python set. Then we’ll check its type. After that, we’ll apply str() function on it and again check its type to see whether it actually works or not. See below code example:
setVal = {1,2,4,5} print( type(setVal) ) convertedVal = str(setVal) print( type(convertedVal) )
Output
<class 'set'> <class 'str'>
We can see that the type is successfully converted from Python set to string by using str function.
Using join() Function
This function is used to combine the items of an iterable with a specified separator. We can specify a separator if we want. See below example:
s = {2,5,7,9,6} print( type(s) ) cVal = '-'.join( str(j) for j in s ) print( type(cVal) ) print( cVal )
Output
<class 'set'> <class 'str'> 2-5-6-7-9
We can see that the type is successfully converted from set to string. Specifying a separator totally depends upon your choice. Don’t specify anything like this ”.join if you don’t want anything between the items of the resulting string.
Using repr() Function
We can use this function to convert the type of Python set from set to string. See below example:
sVal = {5,3,7,20.5} print( type(sVal) ) conVal = repr(sVal) print( type(conVal) )
Output
<class 'set'> <class 'str'>
Using map() Function
We can use this function in combination with the join function to convert Python set to string. The map function converts each item of an iterable to string by type casting it. We’ll be storing the items in a list using list() function. Finally, we’ll combine these items using join() function. See below example:
aa = {'This','is','Python','Programming'} print( type(aa) ) res=' '.join( list(map(str,aa)) ) // we've specified a space in ' '.join print( type(res) ) print( res )
Output
<class 'set'> <class 'str'> is Python Programming This
Using Python For Loop
This method uses for loop to fetch each item of set, convert it to a string using str() function and store it in a variable. So the result will be a string. See below example:
aa = {2.4,5,3,'Python',34.2} result = '' for val in aa: result = result+str(val) print( result ) print( type(result) )
Output
2.4334.25Python <class 'str'>
So this is how we can easily convert Python set to string using multiple ways. Hope you have learned something new from this post. Do feel free to share it with your developer buddies.
Click here to learn Python set to string conversion in even more detail.
Do ask if you still have any questions regarding the conversion of Python set to string. We’ll be very glad to answer all.
Conclusion
In conclusion, hope you now have an in-depth knowledge of how to easily convert Python set to string. We’d be glad to have your informative feedback.
Do visit our other posts on Python programming to learn about other important Python concepts in detail. Thank you for reading this article.