Python - How To Perform the Bitwise Xor of Two Strings in Python

ID : 57

viewed : 89

Tags : PythonPython String

vote vote


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.


[0, 0, 1, 1, 0, 1] 

  • Related HOW TO?