Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the str.isalpha() method check in a string?
It checks if all characters in the string are letters (a-z or A-Z) and the string is not empty.
Click to reveal answer
intermediate
How does str.isdigit() differ from str.isnumeric()?
str.isdigit() checks if all characters are digits (0-9), while str.isnumeric() also returns True for numeric characters like fractions or superscripts.
Click to reveal answer
beginner
What does str.isspace() check for in a string?
It returns True if the string contains only whitespace characters like spaces, tabs, or newlines, and is not empty.
Click to reveal answer
beginner
Explain the use of str.isalnum().
It checks if all characters in the string are letters or numbers (no spaces or symbols) and the string is not empty.
Click to reveal answer
beginner
What will 'Hello123'.isalpha() return and why?
It will return False because the string contains numbers along with letters, and isalpha() requires only letters.
Click to reveal answer
Which method checks if a string contains only letters and is not empty?
Aisalpha()
Bisdigit()
Cisspace()
Disalnum()
✗ Incorrect
isalpha() returns True only if all characters are letters and the string is not empty.
What does str.isdigit() return for the string '12345'?
ATrue
BFalse
CError
DDepends on Python version
✗ Incorrect
Since '12345' contains only digits, isdigit() returns True.
Which method would return True for a string containing only spaces and tabs?
Aisalnum()
Bisnumeric()
Cisalpha()
Disspace()
✗ Incorrect
isspace() returns True if the string contains only whitespace characters.
What will 'abc123'.isalnum() return?
AError
BTrue
CFalse
DNone
✗ Incorrect
isalnum() returns True if all characters are letters or numbers, which is true here.
Which method is best to check if a string contains only numeric characters including fractions or superscripts?
Aisalpha()
Bisdigit()
Cisnumeric()
Disspace()
✗ Incorrect
isnumeric() covers a wider range of numeric characters than isdigit().
Describe how you can check if a string contains only letters or numbers in Python.
Think about a method that allows both letters and digits but no spaces.
You got /4 concepts.
Explain the difference between isdigit() and isnumeric() methods.
Consider what kinds of numeric characters each method accepts.
You got /4 concepts.
Practice
(1/5)
1. Which Python string method checks if all characters in a string are letters (a-z or A-Z)?
easy
A. isdigit()
B. isalpha()
C. isalnum()
D. isspace()
Solution
Step 1: Understand what isalpha() does
The isalpha() 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, and isspace() checks for whitespace characters.
Final Answer:
isalpha() -> Option B
Quick Check:
Check letters only = isalpha() [OK]
Hint: Letters only? Use isalpha() method [OK]
Common Mistakes:
Confusing isalpha() with isdigit()
Using isalnum() which allows digits too
Thinking isspace() checks letters
2. Which of the following is the correct syntax to check if a string s contains only digits in Python?
easy
A. if s.isdigit()
B. if s.isdigit:
C. if isdigit(s)
D. if s.is_digit()
Solution
Step 1: Recall method call syntax in Python
Methods require parentheses to call them, so s.isdigit() is correct.
Step 2: Identify incorrect options
s.isdigit misses parentheses, isdigit(s) is not a built-in function, and s.is_digit() is not a valid method.
Final Answer:
if s.isdigit() -> Option A
Quick Check:
Method call needs () = isdigit() [OK]
Hint: Call string methods with parentheses () [OK]
Common Mistakes:
Forgetting parentheses after method name
Using wrong method names like is_digit()
Trying to call isdigit as a function
3. What will be the output of this code?
text = "Hello123"
print(text.isalnum())
print(text.isalpha())
medium
A. True\nTrue
B. False\nFalse
C. True\nFalse
D. False\nTrue
Solution
Step 1: Check text.isalnum()
The string "Hello123" contains letters and digits only, so isalnum() returns True.
Step 2: Check text.isalpha()
Since the string contains digits, isalpha() returns False.
Final Answer:
True\nFalse -> Option C
Quick Check:
Letters+digits = True, letters only = False [OK]
Hint: isalnum() allows digits; isalpha() does not [OK]
A. The string has spaces, so isdigit() returns False
B. The method isdigit() is misspelled
C. The print statement is missing parentheses
D. The variable user_input is not defined
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 A
Quick Check:
Spaces cause isdigit() to fail [OK]
Hint: Spaces cause isdigit() to return False [OK]
Common Mistakes:
Ignoring spaces in the string
Thinking isdigit() ignores whitespace
Assuming method name is wrong
5. You want to check if a string s contains only printable characters and no spaces. Which combination of methods correctly validates this?
hard
A. s.isprintable() and s.isalnum()
B. s.isprintable() and not s.isspace()
C. s.isprintable() and not s.isalpha()
D. s.isprintable() and not ' ' in s
Solution
Step 1: Understand isprintable()
This method returns True if all characters are printable (letters, digits, punctuation, etc.) including spaces.
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 s uses not ' ' in s to exclude spaces, combined with isprintable(), which is correct. s.isprintable() and not s.isspace() fails because not 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 D
Quick Check:
Exclude spaces by checking ' ' not in string [OK]
Hint: Check printable and exclude spaces with ' ' not in s [OK]