HomeLinuxXOR Two Strings in Python

XOR Two Strings in Python


Whereas programming in Python, the “XOR” operator is utilized in a number of duties. For example, to detect errors or points in information transmission with the assistance of checksum, apply encryption on messages, or retrieve sure bits from a quantity. Extra particularly, it’s used for evaluating two units of information or strings and figuring out if they’re related or have some variations.

This weblog will cowl the next points:

What’s XOR in Python?

In Python, “XOR” or “Unique OR” is an operator that compares two binary numbers bitwise. This operator returns “0” if each bits are the identical. Else, it outputs “1” in case each values are totally different. Python’s built-in XOR operator permits us to logically mix two values with the assistance of the Unique OR operation.

How Does XOR Work in Python?

The XOR operator is represented as “^” in Python, and it follows the given syntax:

Right here, “x” and “y” point out the binary numbers, “^” represents the XOR operator, and “xor_number” shops the resultant Boolean worth.

XOR Reality Desk

To know extra concerning the working of the XOR operator, have a look at the given desk:

x y x ^ y
0 0 0
0 1 1
1 0 1
1 1 0

Easy methods to XOR Two Strings in Python?

As XOR is a bitwise operator, it may possibly solely function on integers. Nevertheless, to carry out the XOR operation between two strings, it’s required to transform them into their respective ASCII/Unicode values utilizing the “ord()” perform after which apply the “^” operator.

Three main case situations exist when contemplating the XOR operator implication on string values:

  • Case 1: Each strings comprise binary values.
  • Case 2: Each strings comprise characters.
  • Case: 3: One string relies on integers/binary values, and the second accommodates characters.

Let’s virtually reveal the said circumstances!

Case 1: Apply XOR on Two Strings Having Binary Values

To use the XOR operator on two strings having binary values, firstly initialize them as follows:

string1 = “0100100”
string2 = “1001010”

Now, add the next strains:

outcome = [(ord(a) ^ ord(b)) for a, b in zip(string1, string2)]
print(outcome)

In response to the given code:

  • Invoke the “zip()” perform with the “for” loop to create pairs of characters “a”, “b”, from the “string1” and “string2”.
  • Every generated pair is then handed to the “ord()” perform to transform them to their respective ASCII values.
  • ^” performs the XOR operation on the transformed ASCII/Unicode values and appends them to the record.
  • Lastly, the resultant worth is saved within the “outcome” variable and displayed utilizing the “print()” perform:

It may be noticed that for every identical index little bit of each strings, the XOR operator outputs “0”, and for various values, “1” is returned.

Case 2: Apply XOR on Two Strings Having Characters

Now, the XOR operator is utilized on two strings having the next character values:

string1 = “two”
string2 = “ten”
outcome = [(ord(a) ^ ord(b)) for a,b in zip(string1, string2)]
print(outcome)

On this case, the “ord()” perform returns the ASCII worth for every generated and go them to “^” to carry out the XOR operation:

One other easiest strategy to keep in mind the working of this case is when the “ord()” perform returns the ASCII worth for every pair, the XOR operator outputs their subtracted worth.

For instance:

  • The primary character of each strings is “t”, whose ASCII worth is “116”, so the XOR operator returned 0 (116-116).
  • The second character of the primary string is “w” with the ASCII worth “119”, and for the second string, the “e” character exists on the identical index having ASCII worth “101”. Subsequently, the XOR operator outputs 18 (119-101).
  • For the final bit, the primary string accommodates “o” with ASCII worth “111,” and the second string includes “n” having “110” ASCII worth. In consequence, the XOR operator returned 1 (111-110).

Case 3: Apply XOR on Two When One String Comprises Characters, and Different is Primarily based on Integers/Binary Values

Final case within the ongoing dialogue is when one string accommodates characters, reminiscent of “two”, and the opposite one includes integers or binary values, like “111”:

string1 = “two”
string2 = “111”
outcome = [chr(ord(a) ^ ord(b)) for a,b in zip(string1, string2)]
print(outcome);

Right here, the code works equally to the second case. Besides that the returned ASCII values have been transformed to the respective characters utilizing the “chr()” Python perform:

The returned record represents the characters of the corresponding ASCII values.

Conclusion

In Python, XOR “^” returns “0” if each bits are the identical. Else, it outputs “1”. Nevertheless, each strings comprise characters, values are transformed into their ASCII illustration utilizing the “ord()” perform, after which the XOR operator is utilized. The identical operation is carried out when each strings comprise totally different information varieties, like characters and integers. Solely the differentiation is to invoke the “chr()” perform to get the resultant worth in characters. This weblog demonstrated learn how to XOR two strings in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments