Where is the logical operator xor that can also be used in a string?

Asked 2 years ago, Updated 2 years ago, 32 views

Compare the two string values and return True when only one is true (None or a string other than ").

^ seems to be a bit operator, not a logical operator, and cannot be written in a string, such as str1^ str2. How can I make the logic_xor() part in the source code below?

str1 = raw_input ("Enter string one:") #Enter
str2 = raw_input ("Enter string two:") #Input
If logical_xor(str1, str2): #This is where you need to make it
    print "ok"
else:
    print "bad"

python logic

2022-09-21 17:11

1 Answers

String xor operation is

I'll take advantage of it.

Here's how to implement logic_xor():

def logical_xor(str1, str2):
    return bool(str1) != bool(str2)

mystr0 = "abcd"
mystr1 = "abc"
mystr2 = ""
mystr3 = None

print logical_xor(mystr0, mystr1) #False
print logical_xor(mystr0, mystr2) #True
print logical_xor(mystr0, mystr3) #True
import operator

def logical_xor(str1, str2):
    return operator.xor(bool(str1), bool(str2))

mystr0 = "abcd"
mystr1 = "abc"
mystr2 = ""
mystr3 = None

print logical_xor(mystr0, mystr1) #False
print logical_xor(mystr0, mystr2) #True
print logical_xor(mystr0, mystr3) #True


2022-09-21 17:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.