第五章:最新版零基础学习 PYTHON 教程—Python 字符串操作指南(第六节 - Python 中字符串的逻辑运算符)
对于 python 中的字符串,布尔运算符(and、or、not)起作用。让我们考虑两个字符串,即 str1 和 str2,并在它们上尝试布尔运算符:
Python3
str1 = ''
str2 = 'geeks'# 使用 repr 打印带引号的字符串# 返回 str1
print(repr(str1 and str2)) # 返回 str1
print(repr(str2 and str1)) # 返回 str2
print(repr(str1 or str2)) # 返回 str2
print(repr(str2 or str1)) str1 = 'for'# 返回 str2
print(repr(str1 and str2)) # 返回 str1
print(repr(str2 and str1)) # 返回 str1
print(repr(str1 or str2)) # 返回 str2
print(repr(str2 or str1)) str1='geeks'# 返回 False
print(repr(not str1)) str1 = '' # 返回 True
p