当前位置:首页 > 行业动态 > 正文

python如何判断字符串相等

在Python中,判断两个字符串是否相等可以使用==运算符,如果两个字符串的字符完全相同(包括顺序和大小写),则它们被认为是相等的。

python如何判断字符串相等  第1张

以下是一个简单的示例:

str1 = "hello"
str2 = "world"
str3 = "hello"
if str1 == str2:
    print("str1 and str2 are equal")
else:
    print("str1 and str2 are not equal")
if str1 == str3:
    print("str1 and str3 are equal")
else:
    print("str1 and str3 are not equal")

输出结果:

python如何判断字符串相等  第2张

str1 and str2 are not equal
str1 and str3 are equal

在这个例子中,我们创建了三个字符串变量str1str2str3,然后我们使用==运算符来比较它们是否相等,如果相等,我们打印出相应的消息,否则打印出不相等的消息。

0