Python 变量与字符串比较:使用 == 和 != 运算符
在 Python 中,您可以使用==运算符来判断一个变量是否等于一个字符串。以下是一个示例:
variable = 'hello'
if variable == 'hello':
print('The variable is equal to 'hello'')
else:
print('The variable is not equal to 'hello'')
在上面的示例中,如果变量variable的值等于字符串'hello',则打印出'The variable is equal to 'hello'';否则,打印出'The variable is not equal to 'hello''。
另外,如果您想要检查变量是否不等于一个字符串,可以使用!=运算符。以下是一个示例:
variable = 'hello'
if variable != 'hello':
print('The variable is not equal to 'hello'')
else:
print('The variable is equal to 'hello'')
在上面的示例中,如果变量variable的值不等于字符串'hello',则打印出'The variable is not equal to 'hello'';否则,打印出'The variable is equal to 'hello''。
原文地址: https://www.cveoy.top/t/topic/pV5q 著作权归作者所有。请勿转载和采集!