How to Perform the Bitwise Xor of Two Strings in Python
Najwa Riyaz
Feb 02, 2024
Python
Python String

This article shows you how to perform bitwise exclusive or of two strings in Python.
Use the ^
Operator to Perform the Bitwise Exclusive OR
of Two Strings in Python
You can use the ^
operator to perform Bitwise XOR
strings in Python. The example below illustrates this.
s1 = "100001"
s2 = "101100"
l = [ord(a) ^ ord(b) for a, b in zip(s1, s2)]
print(l)
The zip()
function takes the two strings and aggregates them in a tuple. Here, the ord()
function returns the integer representing the characters in the byte string.
Next, we use the ^
XOR operator between the two strings to perform the Bitwise Exclusive OR
operation on their respective binary representations.
Output:
[0, 0, 1, 1, 0, 1]
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe