isalpha, isalnum, isspace Classify Characters

Gate input by character class with isalpha, isalnum, and isspace — all three return False for empty strings, so strip before judging user input.

PythonForm Validation

Code

pythonsnippet
14 lines
samples = ["hello", "hello123", "123", "  ", "café", ""]

for s in samples:
    print(repr(s), "alpha:", s.isalpha(), "alnum:", s.isalnum(), "space:", s.isspace())

# isalpha 对组合字符返回 False:é 是字母但 café 整体仍 True
print("café.isalpha():", "café".isalpha())

# 空串全部 False:用 .strip() 后再判定更安全
raw = "  abc  "
print(raw.isalpha(), raw.strip().isalpha())

# isalnum 包含字母和数字,但不包含标点和空格
print("v2.0".isalnum(), "v2_0".isalnum())

Output

Captured from a sandboxed run of this snippet in the build pipeline.

'hello' alpha: True alnum: True space: False
'hello123' alpha: False alnum: True space: False
'123' alpha: False alnum: True space: False
'  ' alpha: False alnum: False space: True
'café' alpha: True alnum: True space: False
'' alpha: False alnum: False space: False
café.isalpha(): True
False True
False False

How it works

The file repeats itself only inside for s in samples.

Everything operates on top-level bindings: samples, raw.

Caveats

  • Self-contained — no imports beyond what the runtime already provides.

Applying it

The file’s steps show up in the output as café.isalpha():, v2.0.

Statements that matter

for s in samples:
    print(repr(s), "alpha:", s.isalpha(), "alnum:", s.isalnum(), "space:", s.isspace())

Interactive Tools for This Topic

FAQ

Are there any imports to resolve outside the language?
No. Every import resolves against the language's own modules. Its state lives in module-level bindings: `samples`, `raw`.