Home > Article > Backend Development > Python program to compare two strings by ignoring case
In Python, we can use comparison operators such as "==", "!=", "", "=" and Python built-in functions. For example, lower() and upper() methods compare two strings by ignoring case. A string is a sequence of characters enclosed in double quotes. These operators compare strings based on the Unicode code point assigned to each character of the string. In this article, we will learn how to compare two strings by ignoring the case of the strings.
To compare two strings in Python and ignore case, we can use lower() or upper() function to convert the string to lowercase or uppercase respectively. Once the strings are fully converted to lowercase or uppercase, we can compare strings ignoring their case.
In the following example, we use the lower() method to convert the string to lowercase. Then we compare the two strings using the "==" operator. Since both strings are identical, the output of the code will be "Strings equal, ignoring case".
string1 = "Hello" string2 = "hello" if string1.lower() == string2.lower(): print("The strings are equal, ignoring case.") else: print("The strings are not equal, ignoring case.")
The strings are equal, ignoring case.
We can also prompt the user to enter their own string for comparison. In the following example, we take two strings and convert both strings to lowercase using the lower() function and then compare the two strings using the "==" operator.
string1 = "welcome To tutorials Point" string2 = "Welcome to Tutorials point" if string1.lower() == string2.lower(): print("The strings are equal, ignoring case.") else: print("The strings are not equal, ignoring case.")
The strings are equal, ignoring case.
Comparing strings in Python can be done by using the Python built-in functions lower() and upper(), which convert strings to lowercase and uppercase respectively before comparison. This case-insensitive comparison is widely used in many operations in Python. In this article, we learned how to compare strings by ignoring their case.
The above is the detailed content of Python program to compare two strings by ignoring case. For more information, please follow other related articles on the PHP Chinese website!