How do we convert a string to a set in Python?


Python’s standard library contains built-in function set() which converts an iterable to set. A set object doesn’t contain repeated items. So, if a string contains any character more than once, that character appears only once in the set object. Again, the characters may not appear in the same sequence as in the string as set() function has its own hashing mechanism

>>> set("hello")
{'l', 'h', 'o', 'e'}


Updated on: 25-Feb-2020

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements