Search code examples
pythonpython-3.xsetiterable

How to put string in a set as an individual item?


I have a string

sample = "http://www.stackoverflow.com"

I want convert this string to a set

final = {"http://www.stackoverflow.com"}

I tried the following code:

final = set(sample)

But i got the wrong out as

{u'.', u'/', u':', u'a', u'b', u'c', u'e', u'h', u'i', u'k', u'l', u'n', u'p', u's', u't', u'w'}

I also used

final  = ast.literal_eval(Sample)

and I got this

SyntaxError: invalid syntax

Is there any other solution for this


Solution

  • 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'}