String validation checks in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we check if a string meets certain rules, like only letters or digits, the time it takes depends on the string's length.
We want to know how the time grows as the string gets longer.
Analyze the time complexity of the following code snippet.
def is_alpha(s):
for char in s:
if not char.isalpha():
return False
return True
This code checks if every character in the string is a letter. It stops early if it finds a non-letter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character in the string.
- How many times: Up to once per character, depending on when a non-letter is found.
As the string gets longer, the function may check more characters, up to the full length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 character checks |
| 100 | Up to 100 character checks |
| 1000 | Up to 1000 character checks |
Pattern observation: The number of checks grows roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to check grows in a straight line with the string length.
[X] Wrong: "The check always takes the same time no matter the string length."
[OK] Correct: The function looks at each character until it finds a problem or finishes, so longer strings usually take more time.
Understanding how string checks scale helps you write efficient code and explain your reasoning clearly in interviews.
"What if we changed the check to look for digits instead of letters? How would the time complexity change?"
Practice
Solution
Step 1: Understand what
Theisalpha()doesisalpha()method returns True if all characters in the string are alphabet letters only.Step 2: Compare with other methods
isdigit()checks for digits only,isalnum()checks for letters or digits, andisspace()checks for whitespace characters.Final Answer:
isalpha() -> Option BQuick Check:
Check letters only = isalpha() [OK]
- Confusing isalpha() with isdigit()
- Using isalnum() which allows digits too
- Thinking isspace() checks letters
s contains only digits in Python?Solution
Step 1: Recall method call syntax in Python
Methods require parentheses to call them, sos.isdigit()is correct.Step 2: Identify incorrect options
s.isdigitmisses parentheses,isdigit(s)is not a built-in function, ands.is_digit()is not a valid method.Final Answer:
if s.isdigit() -> Option AQuick Check:
Method call needs () = isdigit() [OK]
- Forgetting parentheses after method name
- Using wrong method names like is_digit()
- Trying to call isdigit as a function
text = "Hello123" print(text.isalnum()) print(text.isalpha())
Solution
Step 1: Check
The string "Hello123" contains letters and digits only, sotext.isalnum()isalnum()returns True.Step 2: Check
Since the string contains digits,text.isalpha()isalpha()returns False.Final Answer:
True\nFalse -> Option CQuick Check:
Letters+digits = True, letters only = False [OK]
- Assuming isalpha() returns True with digits
- Confusing isalnum() with isalpha()
- Ignoring that digits affect isalpha()
user_input = " 123 "
if user_input.isdigit():
print("Digits only")
else:
print("Not digits only")Solution
Step 1: Understand what isdigit() checks
isdigit() returns True only if all characters are digits without spaces or other characters.Step 2: Analyze the string " 123 "
The string contains spaces, so isdigit() returns False, causing the else branch to run.Final Answer:
The string has spaces, so isdigit() returns False -> Option AQuick Check:
Spaces cause isdigit() to fail [OK]
- Ignoring spaces in the string
- Thinking isdigit() ignores whitespace
- Assuming method name is wrong
s contains only printable characters and no spaces. Which combination of methods correctly validates this?Solution
Step 1: Understand
This method returns True if all characters are printable (letters, digits, punctuation, etc.) including spaces.isprintable()Step 2: Exclude spaces explicitly
Since spaces are printable, to ensure no spaces, check that the space character ' ' is not in the string.Step 3: Evaluate options
s.isprintable() and not ' ' in susesnot ' ' in sto exclude spaces, combined withisprintable(), which is correct.s.isprintable() and not s.isspace()fails becausenot s.isspace()only checks if the whole string is not spaces, but allows spaces mixed with other chars. Options C and D do not correctly exclude spaces.Final Answer:
s.isprintable() and not ' ' in s -> Option DQuick Check:
Exclude spaces by checking ' ' not in string [OK]
- Using isspace() to exclude spaces incorrectly
- Assuming isprintable() excludes spaces
- Using isalnum() which excludes punctuation
