| by Arround The Web | No comments

Python Set to String

Sets and strings are both collections of different items, but with major differences in their working. A set is a collection of values with no repetition, whereas a string is nothing but a collection of characters with repetitions allowed.

Converting one data type to another is something that every beginner/intermediate programmer should know how to perform. One such conversion is converting a Python set into a Python string. The feat can be easily achieved by using the repr() method, the join() method, and the str() type casting.

Method 1: Using the repr() Method()

The repr() method is used to convert any data type into something that is in a printable format, and that is actually a string data type. That is how we can simply use it to convert a set into a string in Python, to demonstrate this, use the following lines of code:

setVar = {"This","is","LinuxHint","Python"}
#Conversion
stringVar = repr(setVar)

#Print the Output
print(setVar, type(setVar))
print(stringVar, type(stringVar))

 
In this code snippet:

    • A set is created and passed to the repr() method.
    • The result of the repr() method is stored inside the “stringVar” variable.
    • The print() method is used to print the values of both stringVar and the setVar variables along with their type.

When the above code is executed, it produces the following results in the terminal:


Note: Don’t be confused with the sequence of the items of the set and the string in the output, because a set is an “unordered” collection.

The output verifies that the values returned are both objects. However, one of the objects belongs to the “set” class and after the conversion, the new object belongs to the “str” or string class.

Method 2: Using the join() Method

The join() method can be used to convert a set into a string in Python, by specifying the joining delimiter and then passing the values of every item of the set into the argument of the join method. The joining delimiter is something that confuses people a lot.

Well, in a set, each item is separated by a comma and when these items are all merged or in this case “joined” into a string, we use a delimiter to separate each item. To demonstrate the use of the join method for set-to-string conversion, take the following code:

setVar = {"This","is","LinuxHint","Python"}

#Conversion
stringVar = ", ".join(item for item in setVar)

#Printing both variables
print(setVar, type(setVar))
print(stringVar, type(stringVar))

 
Here in this code snippet, a comma, and a blank space “, “ is used as a delimiter and the values of the set are passed into the join method through a for-in loop. The output of this code snippet is as follows:


Note: Don’t be confused with the sequence of the items of the set and the string in the output, because a set is an “unordered” collection.

The output verifies that the set has been successfully converted into a string with the help of the join method. Additionally, the user can also use the map() method within the join() method as well.

Method 3: Using the str() Method

The str() method is used to convert another data type into a string, which is essentially known as type casting. To demonstrate this, take the following code snippet:

setVar = {"This","is","LinuxHint",12,"Python"}

#Conversion
stringVar = str(setVar)

#Printing both variables
print(setVar, type(setVar))
print(stringVar, type(stringVar))

 
The working of this method is very similar to that of the repr() method. When this code is executed, it produces the following results on the terminal:


Note: Don’t be confused with the sequence of the items of the set and the string in the output, because a set is an “unordered” collection.

The output confirms that the set has been converted into a string using the str() type casting method.

Conclusion

Converting a set into a string in Python is something that may seem very daunting at first, but in reality, it is quite an easy job. To do this conversion, the user can utilize built-in methods like the repr() method, the str() type conversion method, and the join() method. To verify that a set has been successfully converted into a string, the user can use the type() method to verify.

Share Button

Source: linuxhint.com

Leave a Reply