Logical Operations on String in Python Last Updated : 05 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like 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: Python 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 str2 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'FalseTrueTime Complexity: O(1) Auxiliary Space: O(1)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.Note that the bitwise operators (|, &) don't work for strings. Logical Operations and Splitting in Strings - Python Programming Comment More infoAdvertise with us Next Article Multiline String in Python K kartik Improve Article Tags : Python Practice Tags : python Similar Reads 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 4 min read Python String Concatenation String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator.Using + OperatorUsing + operator allows us to concatenation or join s 3 min read Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double 4 min read String Comparison in Python Python supports several operators for string comparison, including ==, !=, <, <=, >, and >=. These operators allow for both equality and lexicographical (alphabetical order) comparisons, which is useful when sorting or arranging strings.Letâs start with a simple example to illustrate the 3 min read numpy string operations | istitle() function numpy.core.defchararray.istitle(arr) function returns True for each element in the array if the element is a titlecased string and there is at least one character, it returns false otherwise. Parameters: arr : array_like of str or unicode Returns : [ndarray] Output array of bools. Code #1: Python3 # 1 min read numpy string operations | isspace() function numpy.core.defchararray.isspace(arr) function returns true for each element if there are only whitespace characters in the string and there is at least one character.It returns false otherwise. Syntax: numpy.core.isspace(arr) Parameters: arr : array_like of str or unicode. Returns : [ndarray] Output 2 min read Like