Learn how to compare the length of two strings in Python using len() and equality operators.
Learn how to convert minutes into seconds in Python using arithmetic multiplication and basic unit conversions.
Create a function comp(str1, str2) that returns True if two strings have the same number of characters, and False otherwise.
Length comparison is used in password validation rules, string anagram matching, and data alignment algorithms.
Compare len(str1) == len(str2):
def comp(str1, str2):
return len(str1) == len(str2)
# Testing our function
print(comp("AB", "CD")) # Output: True (2 == 2)
print(comp("ABC", "DE")) # Output: False (3 != 2)
print(comp("hello", "edabit"))# Output: False (5 != 6)
In Python, len() runs in O(1) constant time because string objects store their character length metadata directly in CPython structure headers.
str1 == str2 compares character content, NOT length! Ensure you wrap both strings with len().Use len() to compare collection counts. Next, let's concatenate first and last names!