Why can't I xor strings?

Alex Martelli aleaxit at yahoo.com
Mon Oct 11 09:24:43 CEST 2004

Andrew Dalke <adalke at mindspring.com> wrote:

> Grant Edwards:
> > No, he explained exactly what he was trying to do, and it had
> > nothing to do with encryption.  He wants to know if exactly one
> > (1) of the strings is the empty string.
> BTW, another way to get that is
> if bool(str1) + bool(str2) == 1:
>    print "one and only one of them was empty"

If this is getting into a many-ways-to-skin-the-cat context, I want to
make sure I get my entry in:

    [str1, str2].count('') == 1

Other possibilities (I think -- untested, and it's late here):

    len([x for x in (str1, str2) if x]) == 1

    '' == min(str1, str2) != max(str1, str2)

    len(str1+str2) == max(len(str1),len(str2)) != 0

    sum(map(bool, (str1, str2))) == 1

    not str1 ^ not str2

    (str1 or str2) and not min(str1, str2)

    not str1 != not str2

    str1 != str2 and (str1+str2) == (str2+str1)

I guess I'd better stop here because I can't actually _prove_ the last
one of these actually implies one of the strings is empty but I can't
find counterexamples either...!-)


More information about the Python-list mailing list