September 02, 2024
Python String Compare
Comparison of two strings in Python is very easy task. Like integer values you can also compare string objects with the help of equal to operator (==) inside of if conditional statement. It is as easy as you are comparing two values. The example of string comparison mentioned below:
Code
# Python String Compare
def compare_two_string(str1, str2):
if str1 == str2 :
print('Strings are equal')
return true
else :
print('Strings are not equal')
return false
# Calling function
compare_two_string('Hello', 'world')
compare_two_string('Hello', 'Hello')
In above example, we have created a function with the name of 'compare_two_string' which required two string parameters for comparison. We called compare_two_string function to compare two string if strings are found equal then it shows message 'strings are equal' and returns true value. And if strings are not equal then it prints message that 'String are not equal.' and returns false.