![]() |
VOOZH | about |
There are multiple ways of converting a String to a Set in python, here are some of the methods.
The easiest way of converting a string to a set is by using the set() function.
Example 1 :
<class 'str'>
Geeks
<class 'set'>
{'s', 'e', 'k', 'G'}
Let's explore other methods of converting string to set:
Table of Content
To convert words from a sentence into a set, use spilt() method before set().
Example:
{'for', 'Geeks', 'Geek'}
We can also convert a string to a set by using dict.fromkeys() as it creates a dictionary from an iterable and sets the values to none for every key.
Example:
{'l', 'v', 'o', 'd', 'e', 'p', 'r'}