上一篇
python如何判断字符串相等
- 行业动态
- 2024-04-13
- 4
在Python中,判断两个字符串是否相等可以使用==
运算符,如果两个字符串的字符完全相同(包括顺序和大小写),则它们被认为是相等的。
以下是一个简单的示例:
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")
输出结果:
str1 and str2 are not equal str1 and str3 are equal
在这个例子中,我们创建了三个字符串变量str1
、str2
和str3
,然后我们使用==
运算符来比较它们是否相等,如果相等,我们打印出相应的消息,否则打印出不相等的消息。