XOR on String
Since strings are a sequence, the datatype needs to be normalized for the operation to be performed on them. Therefore, the strings would be converted to bool, and then the xor operation could be performed on them. But due to this, the result of the operation would be binary, i.e., it would result in either True or False (unlike xor of integers where resultant value is produced).
Example: Firstly two strings are defined. One of them is an empty string. Then the strings are converted to the boolean datatype, and the xor operation is performed on them. The result is displayed.
# First string
a = "Hello World!"
# Second string
b = ""
# Performing the xor operation
xor = bool(a) ^ bool(b)
print(xor)
Output:
True
Time complexity: O(n)
Space complexity: O(n), where n is length of string
XOR of Two Variables in Python
The XOR or exclusive is a Boolean logic operation widely used in cryptography and generating parity bits for error checking and fault tolerance. The operation takes in two inputs and produces a single output. The operation is bitwise traditionally but could be performed logically as well. This article will teach you how to get the logical XOR of two variables in Python.