Python set – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Sat, 25 Nov 2023 05:41:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 Python set – Java2Blog https://java2blog.com 32 32 Convert Set to String in Python https://java2blog.com/convert-set-to-string-python/?utm_source=rss&utm_medium=rss&utm_campaign=convert-set-to-string-python https://java2blog.com/convert-set-to-string-python/#respond Sun, 09 Jan 2022 12:05:28 +0000 https://java2blog.com/?p=18735 Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python.

Ways to convert set to string in Python

This tutorial will discuss how to convert a set to string and view it as the latter in Python. We can verify the result using the type() function.

Using the str() function

In Python, we use the str() function to typecast objects and convert them to strings. Internally, it invokes the __str__() function.

For example,

a = set({5,6,8})
s = str(a)
print(s, type(s))

Output:

{8, 5, 6}

In the above example,

  • The set() function defines a set object.
  • We convert it to a string using the str() function.
  • We verify this using the type() function.

Using the repr() function

The repr() function returns the official string representation of an object. Unlike the str() function, it invokes the __repr__() method internally. We can evaluate this string as Python code using the eval() function.

We can use this method to convert a set to a string.

For example,

a = set({5,6,8})
s = repr(a)
print(eval(s), type(s))

Output:

{8, 5, 6}

In the above example, if the source of the string is unknown then the eval() function is not considered a safe option and should be avoided. The ast.literal_eval() is a safer alternative.

Using the join() function

The join() function combines the elements of an iterable based on a specified delimiter. The final result is a string.

We can combine elements of the set using this function and view the final string.

See the code below.

a = set({5,6,8})
s = ', '.join(str(i) for i in a)
print(s, type(s))

Output:

8, 5, 6

In the above example, we typecast every element to a string using the str() function before combining them using the join() function.

Using the map() function

The map() function will apply a function to all the methods of an iterable. We can typecast every element into a string and combine them into a final string.

See the code below.

a = set({5,6,8})
s = ', '.join(list(map(str,a)))
print(s, type(s))

Output:

8, 5, 6

In the above example,

  • We apply the str() function to typecast every element to a string using the map() function.
  • We store the above mentioned elements to a list.
  • Then, we combine the elements using the join() function.

Conclusion

In this tutorial, we discussed how to convert a set to a string. The str() and repr() functions are the most straightforward methods to achieve this conversion. The join() and map() methods are a little complicated but work nevertheless.

]]>
https://java2blog.com/convert-set-to-string-python/feed/ 0
Python create empty set https://java2blog.com/python-create-empty-set/?utm_source=rss&utm_medium=rss&utm_campaign=python-create-empty-set https://java2blog.com/python-create-empty-set/#respond Sat, 21 Dec 2019 18:14:05 +0000 https://java2blog.com/?p=8231 In this post, we will see how to create an set in python.

Set is collection which contains unique elements.You can simply use set() function to create empty set in Python.

Let’s see this with the help of example.

s = set()
print("empty set s:",s)
s.add(1)
s.add(2)
s.add(1)
s.add(3)
s.add(2)

print("set s:",s)

Output:

empty set s: set()
set s: {1, 2, 3}

That’s all about Python create empty set.

]]>
https://java2blog.com/python-create-empty-set/feed/ 0