Python

XOR Two Strings in Python

You may have used many logical, arithmetic, and comparison operators within mathematics and programming while working. One of the frequently used logical operators is the XOR operator. It returns exactly the opposite of the result of the OR operator. Within this article, we will be using the XOR operator on two string-type variable values while working in a Python environment. Let’s have some examples in the Ubuntu 20.04 system.

Example 1

Let’s have a very first example to illustrate the working of the XOR operator on two string-type values. So, we will try to use the Bitwise XOR on two of the bit-type string values. Start with the creation of a new python file in the shell. Use the touch keyword with the name of a file. You can use any editor to open and create a code in the file. So, we have been using the GNU Nano editor as per the below-shown instructions in the image.

$ touch xor.py
$ nano xor.py

After opening the file, we have added the python3 support at the top of it. Two string-type variables v1 and v2 have been initialized with bit values. We have been using another variable to collect the resultant value of applying XOR on both variables. Within this method, the zip functions have been especially used to make the tuple of both string variables.

The ord() method has been applied to each byte of a string of both variables separately to take one byte each time and applied the XOR using the “^” operator on both variables’ bytes. This means the XOR has been applied to the same indexes of both strings at the same time. The newly generated XOR values would be printed out at the shell with the help of a new variable.

#!/usr/bin/python3
v1 =100100
v2 =101010
new = [(ord(a) ^ ord(b)) for a,b in zip(v1, v2)]
print(new)

After running this simple code with the python3 package, we have got the XOR or both strings.

$ python3 xor.py

Example 2

Let’s have another example to use the same zip() and ord() function to calculate the XOR of two strings with the help of a “^” operator in python code. In this type, we have been using simple character strings instead of byte strings in our code. So, open the file and update the string values as shown below. The rest of the code is similar to the above example code.

#!/usr/bin/python3
v1 = “one”
v2 = “two”
new = [(ord(a) ^ ord(b)) for a,b in zip(v1, v2)]
print(new)

After executing this updated file with the python3 support package, we have got the below result. The result is quite different in the case of string-type character values. You can see we have got the XOR of string characters as some integer values.

$ python3 xor.py

Example 3

Within this example, we will be using the characters and numbers in a string variable. So, create two string variables with characters and numbers. The first string contains numbers while the other string variable contains characters only. The same method is used to calculate the XOR of both string variables and their values in a sequence.

This method is taking all the values of both strings as characters. The print statement has been displaying the XOR of both variables on the shell with the helo of the new variable. On the other hand, if you want to join the calculated XOR values of both variables, you can do so by using the “join()” function as shown. The print statement is again used to display the joined XOR.

#!/usr/bin/python3
v1 =123
v2 = “abc”
new = [chr(ord(a) ^ ord(b)) for a,b in zip(v1, v2)]
v3 = “”.join(new)
print(new)

On the execution of the updated code, we have got the resultant XOR as below.

$ python3 xor.py

Conclusion

This article contains an explanation of calculating the XOR of two string values in Python. We have utilized the iterator method with the zip() method, ord() method, join() method, and “^” operator for this purpose. We hope it will be helpful to you.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content