Python

XOR Two Strings in Python

While programming in Python, the β€œXOR” operator is utilized in several tasks. For instance, to detect errors or issues in data transmission with the help of checksum, apply encryption on messages, or retrieve certain bits from a number. More specifically, it is used for comparing two sets of data or strings and determining if they are similar or have some differences.

This blog will cover the following aspects:

What is XOR in Python?

In Python, β€œXOR” or β€œExclusive OR” is an operator that compares two binary numbers bitwise. This operator returns β€œ0” if both bits are the same. Else, it outputs β€œ1” in case both values are different. Python’s built-in XOR operator permits us to logically combine two values with the help of the Exclusive OR operation.

How Does XOR Work in Python?

The XOR operator is represented as β€œ^” in Python, and it follows the given syntax:

xor_number = x ^ y

Here, β€œx” and β€œy” indicate the binary numbers, β€œ^” represents the XOR operator, and β€œxor_number” stores the resultant Boolean value.

XOR Truth Table

To know more about the working of the XOR operator, look at the given table:

x y x ^ y
0 0 0
0 1 1
1 0 1
1 1 0

How to XOR Two Strings in Python?

As XOR is a bitwise operator, it can only operate on integers. However, to perform the XOR operation between two strings, it is required to convert them into their respective ASCII/Unicode values using the β€œord()” function and then apply the β€œ^” operator.

Three major case scenarios exist when considering the XOR operator implication on string values:

  • Case 1: Both strings contain binary values.
  • Case 2: Both strings comprise characters.
  • Case: 3: One string is based on integers/binary values, and the second contains characters.

Let’s practically demonstrate the stated cases!

Case 1: Apply XOR on Two Strings Having Binary Values

To apply the XOR operator on two strings having binary values, firstly initialize them as follows:

string1 = "0100100"
string2 = "1001010"

Now, add the following lines:

result = [(ord(a) ^ ord(b)) for a, b in zip(string1, string2)]
print(result)

According to the given code:

  • Invoke the β€œzip()” function with the β€œfor” loop to create pairs of characters β€œa”, β€œb”, from the β€œstring1” and β€œstring2”.
  • Each generated pair is then passed to the β€œord()” function to convert them to their respective ASCII values.
  • β€œ^” performs the XOR operation on the converted ASCII/Unicode values and appends them to the list.
  • Lastly, the resultant value is stored in the β€œresult” variable and displayed using the β€œprint()” function:

It can be observed that for each same index bit of both strings, the XOR operator outputs β€œ0”, and for different values, β€œ1” is returned.

Case 2: Apply XOR on Two Strings Having Characters

Now, the XOR operator is applied on two strings having the following character values:

string1 = "two"
string2 = "ten"
result = [(ord(a) ^ ord(b)) for a,b in zip(string1, string2)]
print(result)

In this case, the β€œord()” function returns the ASCII value for each generated and pass them to β€œ^” to perform the XOR operation:

Another simplest way to remember the working of this case is when the β€œord()” function returns the ASCII value for each pair, the XOR operator outputs their subtracted value.

For example:

  • The first character of both strings is β€œt”, whose ASCII value is β€œ116”, so the XOR operator returned 0 (116-116).
  • The second character of the first string is β€œw” with the ASCII value β€œ119”, and for the second string, the β€œe” character exists on the same index having ASCII value β€œ101”. Therefore, the XOR operator outputs 18 (119-101).
  • For the last bit, the first string contains β€œo” with ASCII value β€œ111,” and the second string comprises β€œn” having β€œ110” ASCII value. As a result, the XOR operator returned 1 (111-110).

Case 3: Apply XOR on Two When One String Contains Characters, and Other is Based on Integers/Binary Values

Last case in the ongoing discussion is when one string contains characters, such as β€œtwo”, and the other one comprises integers or binary values, like β€œ111”:

string1 = "two"
string2 = "111"
result = [chr(ord(a) ^ ord(b)) for a,b in zip(string1, string2)]
print(result);

Here, the code works similarly to the second case. Except that the returned ASCII values have been converted to the respective characters using the β€œchr()” Python function:

The returned list represents the characters of the corresponding ASCII values.

Conclusion

In Python, XOR β€œ^” returns β€œ0” if both bits are the same. Else, it outputs β€œ1”. However, both strings comprise characters, values are converted into their ASCII representation using the β€œord()” function, and then the XOR operator is applied. The same operation is performed when both strings contain different data types, like characters and integers. Only the differentiation is to invoke the β€œchr()” function to get the resultant value in characters. This blog demonstrated how to XOR two strings in Python.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.