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
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Count words in a given string
- Rearrange a string so that all same characters become d distance away
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?
- How to split a string in C/C++, Python and Java?
- Interesting facts about strings in Python | Set 2 (Slicing)
Improved By : Suchendra Adiga