Logical Operators on String in Python
For strings in python, boolean operators (and, or, not) work. Let us consider the two strings namely str1 and str2 and try boolean operators on them:
Python3
str1 = '' str2 = 'geeks' # repr is used to print the string along with the quotes # Returns str1 print ( repr (str1 and str2)) # Returns str1 print ( repr (str2 and str1)) # Returns str2 print ( repr (str1 or str2)) # Returns str2 print ( repr (str2 or str1)) str1 = 'for' # Returns str2 print ( repr (str1 and str2)) # Returns str1 print ( repr (str2 and str1)) # Returns str1 print ( repr (str1 or str2)) # Returns str2 print ( repr (str2 or str1)) str1 = 'geeks' # Returns False print ( repr ( not str1)) str1 = '' # Returns True print ( repr ( not str1)) # Coded by Nikhil Kumar Singh(nickzuck_007) |
Output:
