python 字符串运算
In Python, the following are the logical operators,
在Python中,以下是逻辑运算符,
Logical AND (and)
逻辑AND( 和 )
Logical OR (or)
逻辑或( 或 )
Logical NOT (not)
逻辑非( 非 )
With strings,
用琴弦
An empty string means False as a Boolean value, while a non-empty string means True as a Boolean value.
空字符串表示False为布尔值,非空字符串表示True为布尔值。
For "and" operator: If the first operand is True, it checks the second operand and returns the second operand.
对于“ and”运算符 :如果第一个操作数为True ,它将检查第二个操作数并返回第二个操作数。
For "or" operator: If the first operand is False, it checks the second operand and returns the second operand.
对于“或”运算符 :如果第一个操作数为False ,它将检查第二个操作数并返回第二个操作数。
For "and" operator: If the operand is an empty string, it returns True; False, otherwise.
对于“ and”运算符 :如果操作数为空字符串,则返回True ; False ,否则。
Example 1:
范例1:
# Logical Operators on String in Python
string1 = "Hello"
string2 = "World"
# and operator on string
print("string1 and string2: ", string1 and string2)
print("string2 and string1: ", string2 and string1)
print()
# or operator on string
print("string1 or string2: ", string1 or string2)
print("string2 or string1: ", string2 or string1)
print()
# not operator on string
print("not string1: ", not string1)
print("not string2: ", not string2)
print()
Output:
输出:
string1 and string2: World
string2 and string1: Hello
string1 or string2: Hello
string2 or string1: World
not string1: False
not string2: False
Example 2:
范例2:
# Logical Operators on String in Python
string1 = "" # empty string
string2 = "World" # non-empty string
# Note: 'repr()' function prints the string with
# single quotes
# and operator on string
print("string1 and string2: ", repr(string1 and string2))
print("string2 and string1: ", repr(string2 and string1))
print()
# or operator on string
print("string1 or string2: ", repr(string1 or string2))
print("string2 or string1: ", repr(string2 or string1))
print()
# not operator on string
print("not string1: ", not string1)
print("not string2: ", not string2)
print()
Output:
输出:
string1 and string2: ''
string2 and string1: ''
string1 or string2: 'World'
string2 or string1: 'World'
not string1: True
not string2: False
翻译自: https://www.includehelp.com/python/logical-operators-on-string.aspx
python 字符串运算