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"

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. 