469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Why can't I xor strings?

I wrote a function to compare whether two strings are "similar" because
I'm using python to make a small text adventure engine and I want to it
to be responsive to slight mispellings, like "inevtory" and the like. To
save time the first thing my function does is check if I'm comparing an
empty vs. a non-empty string, because they are to be never considered
similar. Right now I have to write out the check like this:

if str1 and str2:
if not str1 or not str2:
return 0

Because python won't let me do str1 ^ str2. Why does this only work for
numbers? Just treat empty strings as 0 and nonempty as 1.

Regardless of whether this is the best implementation for detecting if
two strings are similar, I don't see why xor for strings shouldn't be
supported. Am I missing something? Inparticular, I think it'd be cool to
have "xor" as opposed to "^". The carrot would return the resulting
value, while "xor" would act like and/or do and return the one that was
true (if any).
Jul 18 '05
50 5786
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...!-)

str1 = "a"
str2 = "aa"


Jul 18 '05 #51

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Travers Naran | last post: by
7 posts views Thread by temp34k45k | last post: by
23 posts views Thread by arnuld | last post: by
1 post views Thread by Zach | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.