[Type C] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

5. Write a function that receives two string arguments and checks whether they are same-length strings (returns True in this case otherwise false).

Short Answer: The function will compare the lengths of two string arguments and return True if they are of the same length; otherwise, it will return False.

Python
def check_same_length_strings(str1, str2):
    return len(str1) == len(str2)

# Test
print(check_same_length_strings("hello", "world"))  # False
print(check_same_length_strings("apple", "orange"))  # True

Explanation:

  • The function check_same_length_strings compares the lengths of two string arguments using the len() function.
  • It returns True if the lengths are the same, indicating that the strings are of the same length, otherwise, it returns False.

Leave a Reply

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