AppDividend
Latest Code Tutorials

How to Convert Python Set to String

0

Set in Python is a data structure that is used to store unique elements. The string is an array of bytes representing Unicode characters. Python does not have an inbuilt character data type, and that is why a single character is only a string with a length of 1.

Convert Python Set to String

To convert Python set to string, use one of the following methods.

  1. Using str() method
  2. Using string.join() function
  3. Using Python repr() method.

The str() method returns the string version of the object. See the following syntax of the str() method.

Syntax

str(object, encoding=’utf-8?, errors=’strict’)

Parameters

  1. object: The object whose string representation is to be returned.
  2. encoding: Encoding of the given object.
  3. errors: Response when decoding fails.

Return Value

It returns a string version of the given object.

Example

set = {'Vlad', 'Alucard', 'Trevor', 'Isaac', 'Lisa'}
print(set)
print(type(set))

print("After the conversion")
string = str(set)
print(string)
print(type(string))

Output

{'Trevor', 'Alucard', 'Isaac', 'Vlad', 'Lisa'}
<class 'set'>
After the conversion
{'Trevor', 'Alucard', 'Isaac', 'Vlad', 'Lisa'}
<class 'str'>

In this example, we have defined a set and use the type() function to check the variable’s data type.

Using the str() function, we have converted a set into a string.

Converting Python set to string using join() function

Python string join() is an inbuilt function that returns a string in which the items of the sequence have been joined by the str separator.

Syntax

string.join(iterable)

Parameters

The join() method takes an iterable like list, tuple, dict, or set.

Example

set = {'Vlad', 'Alucard', 'Trevor', 'Isaac', 'Carmilla'}
print(set)
print(type(set))

print("After the conversion")
string = ', '.join(set)
print(string)
print(type(string))

Output

{'Isaac', 'Trevor', 'Carmilla', 'Vlad', 'Alucard'}
<class 'set'>
After the conversion
Isaac, Trevor, Carmilla, Vlad, Alucard
<class 'str'>

Using repr() method to convert Python Set to String

Python repr() is an inbuilt function that returns a printable representation of the given object. To safely evaluate an expression node or a Unicode encoded string containing an expression from untrusted sources without the need to parse the values oneself, use the ast.literal_eval() function.

set = {'Vlad', 'Alucard', 'Trevor', 'Isaac', 'Carmilla'}
print(set)
print(type(set))

print("After the conversion")
string = repr(set)
print(string)
print(type(string))

Output

{'Vlad', 'Alucard', 'Trevor', 'Carmilla', 'Isaac'}
<class 'set'>
After the conversion
{'Vlad', 'Alucard', 'Trevor', 'Carmilla', 'Isaac'}
<class 'str'>

When the result from repr() is passed to ast.literal_eval() function, it returns an original object, and in our case, it will be a set. To use the literal_eval() function, import the ast module.

import ast

set = {'Vlad', 'Alucard', 'Trevor', 'Isaac', 'Carmilla'}
print(set)
print(type(set))

print("After the conversion")
string = repr(set)
print(string)
print(type(string))

print("Again after conversion")
st = ast.literal_eval(string)
print(st)
print(type(st))

Output

{'Isaac', 'Trevor', 'Vlad', 'Alucard', 'Carmilla'}
<class 'set'>
After the conversion
{'Isaac', 'Trevor', 'Vlad', 'Alucard', 'Carmilla'}
<class 'str'>
Again after conversion
{'Trevor', 'Isaac', 'Vlad', 'Alucard', 'Carmilla'}
<class 'set'>

First, we defined a set and converted the set into the string using the repr() function, and then in the final output, we get the original set object. The str() and repr() both are used to get a string representation of the object.

The str() is used for creating output for end-user, while repr() is mainly used for debugging and development. The repr() function’s goal is to be unambiguous, and the str() function’s goal is to be readable.

That is it for converting a set to string in Python.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.