How do I compare two strings in python?

user3064366 picture user3064366 · Feb 20, 2014 · Viewed 580.8k times · Source

I have two strings like

string1="abc def ghi"

and

string2="def ghi abc"

How to get that this two string are same without breaking the words?

Answer

oxfn picture oxfn · Feb 20, 2014

Seems question is not about strings equality, but of sets equality. You can compare them this way only by splitting strings and converting them to sets:

s1 = 'abc def ghi'
s2 = 'def ghi abc'
set1 = set(s1.split(' '))
set2 = set(s2.split(' '))
print set1 == set2

Result will be

True