Multiple conditions with strings

Use Python’s case-folded comparison for the second result; empty strings are valid inputs.

Write a function that takes two strings as input and returns "Equal" if they are exactly the same, "CaseSensitive" if they differ only in letter case, and "Different" otherwise.

Complete Listing 1. Save the completed code as compare_strings.py and run it to check the supplied assertions.

Listing 1: Starter function and checks for compare_strings.
def compare_strings(str1: str, str2: str) -> str:
    raise NotImplementedError("Replace this line with your code.")


# Exactly the same
got = compare_strings("openai", "openai")
assert got == "Equal", f"'openai', 'openai': expected 'Equal', got {got!r}"

# Same letters, different case
got = compare_strings("hello", "Hello")
assert got == "CaseSensitive", f"'hello', 'Hello': expected 'CaseSensitive', got {got!r}"

# Different letters
got = compare_strings("hello", "world")
assert got == "Different", f"'hello', 'world': expected 'Different', got {got!r}"

# Different letters and different case: still just different
got = compare_strings("Hello", "world")
assert got == "Different", f"'Hello', 'world': expected 'Different', got {got!r}"

# Your case: which pair of strings is most likely to break it?
# got = compare_strings("___", "___")
# assert got == "___", f"'___', '___': expected '___', got {got!r}"
  • assert condition checks a Boolean condition. A true condition produces no output; a false condition raises AssertionError. For example, assert 2 + 3 == 5 passes.
  • text.casefold() returns a new string for comparison without letter case. It leaves the original unchanged. For example, "Hello".casefold() returns "hello".