python 字符串运算_Python中字符串的逻辑运算符

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 字符串运算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值