Open In App

Convert Set to String in Python

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to convert a set to a string in Python. It can done using two ways – 

Method 1: Using str() We will convert a Set into a String in Python using the str() function.

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

Parameters :

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

Returns : String version of the given object

Example 1 : 

python3




# create a set
s = {'a', 'b', 'c', 'd'}
print("Initially")
print("The datatype of s : " + str(type(s)))
print("Contents of s : ", s)
 
# convert Set to String
s = str(s)
print("\nAfter the conversion")
print("The datatype of s : " + str(type(s)))
print("Contents of s : " + s)


Output :

Initially
The datatype of s : <class 'set'>
Contents of s :  {'c', 'd', 'a', 'b'}

After the conversion
The datatype of s : <class 'str'>
Contents of s : {'c', 'd', 'a', 'b'}

Example 2 : 

python3




# create a set
s = {'g', 'e', 'e', 'k', 's'}
print("Initially")
print("The datatype of s : " + str(type(s)))
print("Contents of s : ", s)
 
# convert Set to String
s = str(s)
print("\nAfter the conversion")
print("The datatype of s : " + str(type(s)))
print("Contents of s : " + s)


Output :

Initially
The datatype of s : <class 'set'>
Contents of s :  {'k', 'g', 's', 'e'}

After the conversion
The datatype of s : <class 'str'>
Contents of s : {'k', 'g', 's', 'e'}

Method 2: Using Join() The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator. 

Syntax:

string_name.join(iterable) 

python3




# create a set
s = {'a', 'b', 'c', 'd'}
print("Initially")
print("The datatype of s : " + str(type(s)))
print("Contents of s : ", s)
  
# convert Set to String
S = ', '.join(s)
print("The datatype of s : " + str(type(S)))
print("Contents of s : ", S)


Output:

Initially
The datatype of s : <class 'set'>
Contents of s :  {'c', 'd', 'a', 'b'}
The datatype of s : <class 'str'>
Contents of s :  c, d, a, b

Method 2: Using functools.reduce()

Syntax:

functools.reduce(function, set) 

Approach: 

  • Initialize the set. 
  • Use reduce function which takes a function and sets it as parameters. 
  • The function applies to each item of the set. 
  • Function concatenate the items of the set. 

Python




from functools import reduce
# create a set
s = {'a', 'b', 'c', 'd'}
print("Initially")
print("The datatype of s : " + str(type(s)))
print("Contents of s : ", s)
 
# convert Set to String
S = reduce(lambda a, b : a+', '+b , s)
print("The datatype of s : " + str(type(S)))
print("Contents of s : ", S)


Output:

Initially
The datatype of s : <class 'set'>
Contents of s :  {'a', 'c', 'd', 'b'}
The datatype of s : <class 'str'>
Contents of s :  a, c, d, b

Time complexity: O(N) N is the length of the set over which reduce function iterate.

Space complexity: O(1) Because no extra space is used. 



Previous Article
Next Article

Similar Reads

Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Convert Unicode String to a Byte String in Python
Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular string. In this article, we will explore five diff
2 min read
Convert String to Set in Python
We can convert a string to setin Python using the set() function. Syntax : set(iterable) Parameters : Any iterable sequence like list, tuple or dictionary. Returns : An empty set if no element is passed. Non-repeating element iterable modified as passed as argument. Example 1 : C/C++ Code # create a string str string = &amp;quot;geeks&amp;quot; pri
2 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python | Convert Bytearray to Hexadecimal String
Sometimes, we might be in a problem in which we need to handle unusual Datatype conversions. One such conversion can be converting the list of bytes(byte array) to the Hexadecimal string format in Python. Let's discuss certain Methods in which this can be done. Using format() + join() to Convert Byte Array to Hex String The combination of the above
4 min read
Python | Convert String to tuple list
Sometimes, while working with Python strings, we can have a problem in which we receive a tuple, list in the comma-separated string format, and have to convert to the tuple list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + split() + replace() This is a brute force method to perform this task. In this, we
5 min read
Python | Program to convert a tuple to a string
Given a tuple of characters, Write a python program to convert the tuple into a string. Examples: Input : ('a', 'b', 'c', 'd', 'e') Output : abcde Input : ('g', 'e', 'e', 'k', 's') Output : geeks Approach 1: using simple for loop Create an empty string and using a for loop iterate through the elements of the tuple and keep on adding each element to
4 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert dictionary object into string
The dictionary is an important container and is used almost in every code of day-to-day programming as well as web development with Python. The more it is used, the more is the requirement to master it and hence it's necessary to learn about them. Input: { "testname" : "akshat","test2name" : "manjeet","test3name" : "nikhil"}Output: {"testname": "ak
3 min read
Python | Convert byteString key:value pair of dictionary to String
Given a dictionary having key:value pairs as byteString, the task is to convert the key:value pair to string. Examples: Input: {b'EmplId': b'12345', b'Name': b'Paras', b'Company': b'Cyware' } Output: {'EmplId': '12345', 'Name': 'Paras', 'Company': 'Cyware'} Input: {b'Key1': b'Geeks', b'Key2': b'For', b'Key3': b'Geek' } Output: {'Key1':'Geeks', 'Key
4 min read
Practice Tags :