• Home
  • 4.13 String Comparison

4.13 String Comparison

View Categories

4.13 String Comparison

String comparison is the process of checking whether two strings are equal or determining their alphabetical (lexicographical) order.

Python compares strings using the standard comparison operators such as ==, !=, <, >, <=, and >=.

When comparing strings, Python compares the characters one by one using their Unicode values. If the first characters are the same, Python continues comparing the next characters until a difference is found.

String comparison is commonly used for validating user input, sorting text, checking passwords, and making decisions in programs.


Syntax #

string1 operator string2

Components #

Component Description
string1 First string to compare.
operator Comparison operator (==, !=, <, >, <=, >=).
string2 Second string to compare.

Example 1: Equality Comparison (==) #

main.py

first = "Python"
second = "Python"

print(first == second)

Output #

True

Explanation #

  • Both strings contain the same characters.
  • Python compares each character.
  • Since they are identical, the result is True.

Example 2: Inequality Comparison (!=) #

main.py

first = "Python"
second = "Java"

print(first != second)

Output #

True

Explanation #

  • The strings are different.
  • Therefore, they are not equal.
  • The != operator returns True.

Example 3: Alphabetical Comparison #

main.py

print("Apple" < "Banana")

Output #

True

Explanation #

  • Python compares the first characters.
  • 'A' comes before 'B' in Unicode.
  • Therefore, "Apple" is considered smaller than "Banana".

Example 4: Comparing Strings with Different Cases #

main.py

print("Python" == "python")

Output #

False

Explanation #

  • Python comparisons are case-sensitive.
  • Uppercase P and lowercase p have different Unicode values.
  • Therefore, the strings are not equal.

Example 5: Greater Than Comparison #

main.py

print("cat" > "car")

Output #

True

Explanation #

  • The first two characters (c and a) are the same.
  • Python compares the third characters.
  • 't' comes after 'r', so the result is True.

Example 6: User Input Comparison #

main.py

password = input("Enter password: ")

print(password == "python123")

Sample Input #

python123

Output #

True

Explanation #

  • The user enters a password.
  • Python compares it with the expected string.
  • The comparison returns True only if both strings match exactly.

Example 7: Comparing Prefixes #

main.py

print("abc" < "abcd")

Output #

True

Explanation #

  • The first three characters are identical.
  • "abc" ends before "abcd".
  • Therefore, the shorter string is considered smaller.

Example 8: Comparing Numeric Strings #

main.py

print("100" < "20")

Output #

True

Explanation #

  • Python compares strings character by character.
  • '1' comes before '2'.
  • The comparison is based on characters, not numeric values.

Example 9: Using lower() for Case-Insensitive Comparison #

main.py

first = "Python"
second = "python"

print(first.lower() == second.lower())

Output #

True

Explanation #

  • lower() converts both strings to lowercase.
  • The converted strings become identical.
  • This technique is commonly used for case-insensitive comparisons.

Example 10: Checking the Result Type #

main.py

result = "Python" == "Python"

print(result)
print(type(result))

Output #

True
<class 'bool'>

Explanation #

  • String comparison returns either True or False.
  • The result is of type bool.

Common Mistakes #

1. Assuming Comparisons Ignore Case #

Incorrect

print("Python" == "python")

Incorrect Expectation #

True

Actual Output #

False

Reason

String comparisons are case-sensitive.

Correct

print("Python".lower() == "python".lower())

2. Confusing Numeric Strings with Numbers #

Incorrect

print("100" > "20")

Incorrect Expectation #

True

Actual Output #

False

Reason

Python compares the characters '1' and '2', not the numeric values.

Correct

print(int("100") > int("20"))

3. Using Assignment Instead of Comparison #

Incorrect

text = "Python"

if text = "Python":
    print("Match")

Output #

SyntaxError: invalid syntax

Reason

Use == for comparison.
The = operator is used only for assignment.

Correct

if text == "Python":
    print("Match")

4. Assuming Strings of Different Lengths Cannot Be Compared #

Incorrect

print("abc" < "abcd")

Incorrect Expectation #

Error

Actual Output #

True

Reason

Python can compare strings of different lengths lexicographically.


Best Practices #

  • Use == to check whether two strings are identical.
  • Remember that string comparisons are case-sensitive.
  • Use lower() or upper() when performing case-insensitive comparisons.
  • Convert numeric strings to integers before numeric comparisons.
  • Use descriptive variable names when comparing strings.

Key Points to Remember #

  • Strings can be compared using comparison operators.
  • Python compares strings character by character.
  • Comparisons are based on Unicode values.
  • String comparisons are case-sensitive.
  • Comparison results are of type bool.
  • Numeric strings are compared as text, not numbers.
  • Use lower() or upper() for case-insensitive comparisons.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *