Check If Two Strings Are Equal using Python
Use == operator to test if two given strings are equal or not. You can use != as not equal to operator. It returns false if string matches.
Example
In this example, we initialize two variable with sting and compare if both stings are equal or not.
1 2 3 4 5 6 | x = "Hello" y = "Hello" if x == y: print("Equal") else: print("Not equal") |
Output:
Equal
Another Example
Here is another sample Python program which will prompt user to enter a strings. Then compare the input string with another predefined string.
1 2 3 4 5 6 | name = "Rahul" x = input("Enter your name to confirm: ") if x == name: print("Match found") fi |