Open In App

Logical Operations on String in Python

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 str1
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: 

''
''
'geeks'
'geeks'
'geeks'
'for'
'for'
'geeks'
False
True

Time Complexity: O(1) 
Auxiliary Space: O(1)

The output of the boolean operations between the strings depends on the following things: 

  1. Python considers empty strings as having a boolean value of the ‘false’ and non-empty strings as having a boolean value of ‘true’.
  2. 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
  3. 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. 

 



Similar Reads

NumPy Array - Logical Operations
Logical operations are used to find the logical relation between two arrays or lists or variables. We can perform logical operations using NumPy between two data. Below are the various logical operations we can perform on Numpy arrays: AND The numpy module supports the logical_and operator. It is used to relate between two variables. If two variabl
4 min read
Python 3 - Logical Operators
Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False), and as a result, they return boolean only (True
4 min read
Python Logical Operators
Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss logical operators in Python definition and also loo
5 min read
Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
PyQt5 QSpinBox - Getting Horizontal Logical DPI value
In this article we will see how we can get the horizontal resolution of the device in dots per inch(DPI) of the spin box. Logical dots per inch are used to convert font and user interface elements from point sizes to pixel sizes, and might be different from the physical dots per inch. Note : If the horizontal logical DPI doesn't equal to the physic
2 min read
PyQt5 QSpinBox - Getting Vertical Logical DPI value
In this article we will see how we can get the vertical resolution of the device in dots per inch(DPI) of the spin box. Logical dots per inch are used to convert font and user interface elements from point sizes to pixel sizes, and might be different from the physical dots per inch. Note : If the vertical logical DPI doesn't equal to the physical v
2 min read
What are the Logical Expressions in Sympy?
SymPy is a symbolic mathematics Python package. Its goal is to develop into a completely featured computer algebra system while keeping the code as basic as possible to make it understandable and extendable. The package is entirely written in python language. Logical expressions in sympy are expressed by using boolean functions. sympy.basic.boolean
7 min read
Compute element-wise logical AND, OR and NOT of tensors in PyTorch
In this article, we are going to see how to compute element-wise logical AND, OR, and NOT of given tensors in PyTorch. We can compute this by using the torch.logical_and(), torch.logical_or(), and torch.logical_not() methods. Let's discuss all of them one by one. Compute element-wise with logical AND torch.logical_and() - This method is used to com
4 min read
What is new in Data Interpretation and Logical Reasoning in CAT 2019 ?
The CAT exam is a computer-based exam in India for admission into the MBA programs of various management institutes of the country such as IIM’s, FMS, DMS, , etc. CAT is organized by one of the IIM’s on a rotational basis and consequently, it is notorious for changing the exam schedule or pattern on a regular basis. Currently, CAT is divided into t
6 min read
G-Fact 19 (Logical and Bitwise Not Operators on Boolean)
Most of the languages including C, C++, Java, and Python provide a boolean type that can be either set to False or True. In this article, we will see about logical and bitwise not operators on boolean. Logical Not (or !) Operator in PythonIn Python, the logical not operator is used to invert the truth value of a Boolean expression, returning True i
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg