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. 

Python
# 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.

Similar Reads

XOR of Two Numbers

As XOR is a bitwise operator, it will compare bits of both integers bit by bit after converting them into binary numbers. The truth table for XOR (binary) is shown below:...

XOR on Integers

The integer numbers are first converted into the binary numbers and then each bit is compared with one another. The final answer is then again converted back into the original integer form. The following code demonstrates the usage of a caret for performing the XOR of two integer variables....

XOR on Boolean

The XOR of two boolean variables is quite simple. The output of the XOR operation is either 0 or 1 which represents True or Flase respectively in boolean format. Hence, to get the logical XOR of boolean datatype, either True or False is provided as the input values....

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)....

XOR of Two Variables using Operator Module

Python has an operator module, which provides a set of predefined functions for arithmetic, logical, bitwise and comparison operators. It also provides the XOR function of the bitwise operator which can be used to get the XOR of two variables....

Swapping Two Integers using XOR without Temporary Variable

The XOR bitwise operation in Python can also be used to swap two integers without using the temporary variable. Let us see how this works....