Why can't I xor strings?

Byron DesertLinux at netscape.net
Fri Oct 8 18:03:51 EDT 2004


Hi DataAngel,

Welcome to the Python community!

 From reading your post, it sounds like you are wanting to use XOR for 
"encryption."  The reason why I say this is because there are many other 
ways of comparing the content of two strings to see if they are exactly 
the same.  An example of how to do such a thing is listed below:

	# How to compare two strings to see if they are exact matches.
	name = "Steven"
	name2 = "Steven"
	print name == name2


The result will be "True."

However, the only reason that one might want to use XOR for a string is 
if he or she wanted to use XOR based encryption in order to keep data 
semi-private.

Let me know,

Byron
---

dataangel wrote:
> 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).
> 
> 



More information about the Python-list mailing list