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:
'' '' 'geeks' 'geeks' 'geeks' 'for' 'for' 'geeks' False True
The output of the boolean operations between the strings depends on following things:
- Python considers empty strings as having boolean value of ‘false’ and non-empty string as having boolean value of ‘true’.
- For ‘and’ operator if left value is true, then right value is checked and returned. If left value is false, then it is returned
- For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned.
Note that the bitwise operators (| , &) don’t work for strings.
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 email@example.com. 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
- Python | Splitting operators in String
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
- Division Operators in Python
- Basic Operators in Python
- Increment and Decrement Operators in Python
- Python Membership and Identity Operators | in, not in, is, is not
- Chaining comparison operators in Python
- Inplace vs Standard Operators in Python
- Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
- Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
- Python | Solve given list containing numbers and arithmetic operators
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- String slicing in Python to rotate a string
Improved By : Suchendra Adiga