Python program to compare two strings by ignoring case

PHPz
Release: 2023-08-28 14:53:05
forward
1516 people have browsed it

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.

Compare strings ignoring case

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.

Example 1

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.")
Copy after login

Output

The strings are equal, ignoring case.
Copy after login
Copy after login

Example 2

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.")
Copy after login

Output

The strings are equal, ignoring case.
Copy after login
Copy after login

in conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template