Python – Test for desired String Lengths

When it is required to test for desired string lengths, a simple iteration and the len() method can be used. This technique helps verify that each string in a list matches its corresponding expected length.

Using Basic Iteration

Below is a demonstration of checking string lengths using a for loop ?

strings = ["python", "is", "fun", "to", "learn", "Will", "how"]

print("The list is :")
print(strings)

expected_lengths = [6, 2, 3, 2, 5, 4, 3]

result = True
for index in range(len(strings)):
    if len(strings[index]) != expected_lengths[index]:
        result = False
        break

print("The result is :")

if result == True:
    print("All the strings are of required lengths")
else:
    print("All the strings are not of required lengths")
The list is :
['python', 'is', 'fun', 'to', 'learn', 'Will', 'how']
The result is :
All the strings are of required lengths

Using zip() Function

A more Pythonic approach using zip() to pair strings with their expected lengths ?

strings = ["hello", "world", "python", "code"]
expected_lengths = [5, 5, 6, 4]

print("String list:", strings)
print("Expected lengths:", expected_lengths)

result = all(len(string) == length for string, length in zip(strings, expected_lengths))

if result:
    print("All strings match their required lengths")
else:
    print("Some strings do not match their required lengths")
String list: ['hello', 'world', 'python', 'code']
Expected lengths: [5, 5, 6, 4]
All strings match their required lengths

Using List Comprehension

You can also get detailed information about which strings match their expected lengths ?

strings = ["cat", "elephant", "dog", "bird"]
expected_lengths = [3, 8, 3, 5]

print("Checking each string:")
for i, (string, expected) in enumerate(zip(strings, expected_lengths)):
    actual = len(string)
    match = actual == expected
    print(f"'{string}': expected {expected}, actual {actual}, match: {match}")

# Get list of all matches
matches = [len(s) == l for s, l in zip(strings, expected_lengths)]
print(f"\nAll strings match: {all(matches)}")
Checking each string:
'cat': expected 3, actual 3, match: True
'elephant': expected 8, actual 8, match: True
'dog': expected 3, actual 3, match: True
'bird': expected 5, actual 4, match: False

All strings match: False

Comparison

Method Readability Performance Best For
Basic Loop Good Fast (early exit) Simple validation
zip() + all() Excellent Fast (short-circuit) Pythonic solution
List Comprehension Good Medium Detailed analysis

Explanation

  • A list of strings is defined and displayed on the console.

  • A list of expected lengths is also defined with corresponding indices.

  • The program iterates through both lists simultaneously using indexing or zip().

  • For each string, the actual length is compared with the expected length using len().

  • If any string doesn't match its expected length, the validation fails.

  • The result is displayed with an appropriate message.

Conclusion

Use the basic loop approach for simple validation with early exit. The zip() + all() method is more Pythonic and readable for most use cases.

Updated on: 2026-03-26T01:10:41+05:30

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements