Logical Operators on String in Python

14 Dec, 2021
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 str1print(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 str2print(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 Falseprint(repr(not str1))          str1 = '' # Returns Trueprint(repr(not str1))           # Coded by Nikhil Kumar Singh(nickzuck_007)
Output: 
''
''
'geeks'
'geeks'
'geeks'
'for'
'for'
'geeks'
False
True
The output of the boolean operations between the strings depends on the following things: 
Python considers empty strings as having a boolean value of the ‘false’ and non-empty strings as having a boolean value of ‘true’.For the ‘and’ operator if the left value is true, then the right value is checked and returned. If the left value is false, then it is returnedFor the ‘or’ operator if the left value is true, then it is returned, otherwise, if the left value is false, then the right value is returned.
Python considers empty strings as having a boolean value of the ‘false’ and non-empty strings as having a boolean value of ‘true’.
For the ‘and’ operator if the left value is true, then the right value is checked and returned. If the left value is false, then it is returned
For the ‘or’ operator if the left value is true, then it is returned, otherwise, if the left value is false, then the right value is returned.
Note that the bitwise operators (|, &) don’t work for strings. 
Python Programming Tutorial - Logical Operations and Splitting in Strings | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersPython Programming Tutorial - Logical Operations and Splitting in Strings | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:31•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=m9iv9aM6Om8" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Nikhil Kumar Singh.If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Suchendra Adiga
punamsingh628700
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.