Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 03

String Validation Methods

Learn how to validate strings using isupper(), islower(), isalpha(), isdigit(), isalnum(), and isspace() with visual decision flows.

Beginner20 min

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 True or False. They never modify the string.

Validation flow (remember this first)

Before choosing a validation method, ask: What am I checking?

String validation decision flow

1. isupper()

Returns True only when all alphabet letters are uppercase.

PYTHON isupper True — all uppercase
PYThon isupper False — mixed case
python
print("PYTHON".isupper())   # True
print("PYThon".isupper())   # False
print("python".isupper())   # False
ALL BIG = True memory trick

2. islower()

Returns True only when all letters are lowercase.

python islower True — all lowercase
Python islower False — contains uppercase
python
print("python".islower())   # True
print("Python".islower())   # False
print("PYTHON".islower())   # False

Rule: 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?

123 has no letters so both case checks are False

3. isalpha()

Returns True only when the string contains letters only.

isalpha decision: Apple yes, Apple123 no, spaces break it
python
print("Apple".isalpha())             # True
print("Apple123".isalpha())          # False
print("Guido Van Rossum".isalpha())  # False

Remember: Spaces are not letters.

4. isdigit()

Returns True only when every character is a digit.

12345 isdigit True — digits only
12.34 isdigit False — dot is not a digit
python
print("123".isdigit())       # True
print("12.34".isdigit())     # False
print("Python123".isdigit()) # False

Why decimals fail:

12.34 — dot is not a digit

5. isalnum()

Returns True when the string contains letters and/or digits only.

Python312 isalnum True — letters and digits
Python 312 isalnum False — space breaks it
python
print("Python312".isalnum())   # True
print("Python".isalnum())      # True
print("312".isalnum())         # True

print("Python 312".isalnum())  # False
print("Python3.12".isalnum()) # False

Decision rule:

isalnum: letters yes, numbers yes, spaces no

6. isspace()

Returns True only when every character is whitespace.

three spaces isspace True
python 123 isspace False — contains characters
python
print("   ".isspace())     # True
print("".isspace())        # False
print("python".isspace())  # False

Important: 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

python
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:

Terminal-style validation output preview

Use the lesson playground at the bottom of the page to run this code live.

Quick knowledge check

  1. Does "PYTHON311".isalnum() return True?
    Answer: Yes — it contains only letters and digits.

  2. Does "123 456".isdigit() return True?
    Answer: No — the space breaks it.

  3. When is isspace() True?
    Answer: When every character is whitespace (spaces, tabs, or newlines).

30-second visual revision

Validation methods hub diagram
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…