How to put string in a set as an individual item?
Just do it:
In [1]: s = "http://www.stackoverflow.com"
In [2]: f = {s}
In [3]: type(f)
Out[3]: builtins.set
In [4]: f
Out[4]: {'http://www.stackoverflow.com'}
sample = "http://www.stackoverflow.com"
final = set((sample, ))
The set()
class ,which is also considered a built-in type, accepts an iterable and returns the unique items from that iterable in a set
object. Here since strings are considered a form of iterable --of characters-- you can't just call it on your string. Instead, you can either put the string object literally inside a set while defining it or if you're forced to use set()
you can put it inside another iterable such as list or tuple before you pass it to set()
.
In [14]: s = {'sample string'}
In [15]: s
Out[15]: {'sample string'}
In [16]: s = set(['sample string'])
In [17]: s
Out[17]: {'sample string'}