Lesson 03
String Validation Methods
Learn how to validate strings using isupper(), islower(), isalpha(), isdigit(), isalnum(), and isspace() with visual decision flows.
What you'll learn
| Topic | Methods |
|---|---|
| Case checks | isupper() & islower() |
| Letters only | isalpha() |
| Digits only | isdigit() |
| Alphanumeric & space | isalnum() & isspace() |
Important: These methods only return
TrueorFalse. They never modify the string.
Validation flow (remember this first)
Before choosing a validation method, ask: What am I checking?
1. isupper()
Returns True only when all alphabet letters are uppercase.
print("PYTHON".isupper()) # True
print("PYThon".isupper()) # False
print("python".isupper()) # False2. islower()
Returns True only when all letters are lowercase.
print("python".islower()) # True
print("Python".islower()) # False
print("PYTHON".islower()) # FalseRule: Every letter must be lowercase.
isupper() vs islower()
| String | isupper() |
islower() |
|---|---|---|
"PYTHON" |
True | False |
"python" |
False | True |
"PyThon" |
False | False |
"123" |
False | False |
Why is "123" False for both?
3. isalpha()
Returns True only when the string contains letters only.
print("Apple".isalpha()) # True
print("Apple123".isalpha()) # False
print("Guido Van Rossum".isalpha()) # FalseRemember: Spaces are not letters.
4. isdigit()
Returns True only when every character is a digit.
print("123".isdigit()) # True
print("12.34".isdigit()) # False
print("Python123".isdigit()) # FalseWhy decimals fail:
5. isalnum()
Returns True when the string contains letters and/or digits only.
print("Python312".isalnum()) # True
print("Python".isalnum()) # True
print("312".isalnum()) # True
print("Python 312".isalnum()) # False
print("Python3.12".isalnum()) # FalseDecision rule:
6. isspace()
Returns True only when every character is whitespace.
print(" ".isspace()) # True
print("".isspace()) # False
print("python".isspace()) # FalseImportant: An empty string (
"") is not considered whitespace.
Comparison table
| String | Alpha | Digit | Alnum |
|---|---|---|---|
"Apple" |
Yes | No | Yes |
"123" |
No | Yes | Yes |
"Python312" |
No | No | Yes |
"Python 312" |
No | No | No |
"12.34" |
No | No | No |
Playground example
tests = [
"PYTHON",
"python",
"PYThon",
"123",
"Python312",
" "
]
for s in tests:
print(s)
print("isupper :", s.isupper())
print("islower :", s.islower())
print("isalpha :", s.isalpha())
print("isdigit :", s.isdigit())
print("isalnum :", s.isalnum())
print("isspace :", s.isspace())
print("-" * 20)Visual output idea:
Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
Does
"PYTHON311".isalnum()return True?
Answer: Yes — it contains only letters and digits. -
Does
"123 456".isdigit()return True?
Answer: No — the space breaks it. -
When is
isspace()True?
Answer: When every character is whitespace (spaces, tabs, or newlines).
30-second visual revision
| Method | Easy memory |
|---|---|
isupper() |
All BIG letters |
islower() |
All small letters |
isalpha() |
Alphabet only |
isdigit() |
Numbers only |
isalnum() |
Letters + numbers |
isspace() |
Whitespace only |
Interview shortcut: Alpha → Letters · Digit → Numbers · Alnum → Both · Space → Only whitespace · Upper/Lower → Case validation
Next: Index and Count Methods.
Loading playground…