Perform the Bitwise Xor of Two Strings in Python

Perform the Bitwise Xor of Two Strings in Python

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]

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Extract Numbers From a String in Python
  • Convert String to Datetime in Python