
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Logical Operators on String in Python?
Python logical operator “and” and “or” can be applied on strings. An empty string returns a Boolean value of False. Let’s first understand the behaviour of these two logical operator “and” and “or”.
And operator
Return the first falsey value if there are any, else return the last value in the expression or operator: Return the first truthly value if there are any, else return the last value in the expression.
Operation | Result |
---|---|
X and y | If x is false, then y else x |
X and y | If x is false, then x, else y |
Not x | If x is false, then true, else false |
Below is is the program to demonstrate the use of logical operators on string in python −
str1 = "" str2 = "python" print(repr(str1 and str2)) print(repr(str2 and str1)) print(repr(str1 or str2)) print(repr(str2 or str1)) str1 = "Hello " print(repr(str1 and str2)) print(repr(str2 and str1)) print(repr(str1 or str2)) print(repr(str2 or str1)) print(repr(not str1)) str2 = "" print(repr(not str2)) str2 = "hello" print("Hello == hello: ", str1 == str2)
output
'' '' 'python' 'python' 'python' 'Hello ' 'Hello ' 'python' False True Hello == hello: False
- Related Questions & Answers
- Logical Operators on String in C#
- Logical Operators on String in Java
- Python Logical Operators
- Java Logical Operators
- Perl Logical Operators
- Logical Operators in C++
- Java Regular expressions Logical operators
- What are Logical Operators in JavaScript?
- Relational and Logical Operators in C
- Explain the logical operators in DBMS
- What are the logical operators in Java?
- What are the logical operators in C#?
- Written version of Logical operators in C++
- What types of logical operators are in javascript?
- String Special Operators in Python
Advertisements