Open In App

Put String in A Set in Python

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Imagine you have a magical box in Python that only stores unique items – that’s exactly what a set is! Sets are like special containers designed to hold a bunch of different things without any duplicates. In Python, they provide a versatile and efficient way to manage collections of distinct elements. Now, let’s dive into the magic of sets and explore how effortlessly we can place a single string into them using different methods.

Put String In A Set As An Individual Item

Below, are the examples of How To Put a String In A Set As An Individual Item in Python.

Put String In A Set As An Individual Item Using the set()

In this example, below code converts the string "Hello, World!" into a set named `my_set`, where each character becomes an individual element. It then prints the type of the original string, the resulting set, and the type of the set.

Python3




string = "Hello, World!"
my_set = set(string)
 
print(type(string))
print(my_set)
print(type(my_set))


Output

<class 'str'>
{'!', ',', 'd', 'e', 'l', 'H', ' ', 'o', 'W', 'r'}
<class 'set'>

Put String In A Set As An Individual Item by Adding One Element

In this example, below code initializes a string “Hello, World!”, creates an empty set named `my_set`, adds the entire string as a single element to the set, and then prints the type of the string, the set containing the string, and the type of the set.

Python3




string = 'Hello, World!'
my_set = set()
my_set.add(string)
 
print(type(string))
print(my_set)
print(type(my_set))


Output

<class 'str'>
{'Hello, World!'}
<class 'set'>

Put String In A Set As An Individual Item Using Set Comprehension

In this example, below code creates a set `my_set` using a set comprehension to extract individual characters from the string “Hello, World!”. It then prints the type of the original string, the resulting set of individual characters, and the type of the set.

Python3




string = 'Hello, World!'
my_set = {char for char in string}
 
print(type(string))
print(my_set)
print(type(my_set))


Output

<class 'str'>
{'r', ',', 'e', 'W', ' ', 'H', 'o', 'l', 'd', '!'}
<class 'set'>

Put String In A Set As An Individual Item Using update() Method

In this example, below code initializes a string ‘World!’, a set {‘Hello’}, and then updates the set with the individual characters from the string. It prints the type of the string, the updated set, and the type of the set.

Python3




string = 'World!'
my_set = {'Hello'}
my_set.update(string)
 
print(type(string))
print(my_set)
print(type(my_set))


Output

<class 'str'>
{'l', 'r', '!', 'Hello', 'o', 'd', 'W'}
<class 'set'>

Conclusion

In conclusion, incorporating a string into a set as an individual item is a straightforward process. By utilizing the set() constructor and enclosing the string within square brackets, you can easily create a set with the desired string element. This method ensures uniqueness within the set, as sets only allow distinct elements. Overall, the simplicity of this approach makes it an efficient way to manage and manipulate individual string items within a set in Python.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads