在Python中,判断两个字符串是否相等,可以使用==操作符,也可以使用函数equal()。
使用==操作符
使用==操作符,可以比较两个字符串是否相等,如果相等,返回True,否则返回False。例如:
str1 = "Hello World"
str2 = "Hello World"
if str1 == str2:
print("The two strings are equal.")
else:
print("The two strings are not equal.")
输出结果为:The two strings are equal.
使用equal()函数
equal()函数可以比较两个字符串是否相等,如果相等,返回True,否则返回False。例如:
str1 = "Hello World"
str2 = "Hello World"
if str1.equal(str2):
print("The two strings are equal.")
else:
print("The two strings are not equal.")
输出结果为:The two strings are equal.
以上就是Python中判断两个字符串是否相等的方法,可以使用==操作符或equal()函数,如果相等,返回True,否则返回False。