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

crax picture crax · Nov 28, 2014 · Viewed 29.7k times · Source

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

Answer

user1907906 picture user1907906 · Nov 28, 2014

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