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.
Code
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 FalseHow 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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.