String tests & padding in Python
Inspecting and aligning text with str predicates.
token = "Room101"
is_alnum = token.isalnum()
has_digit = any(c.isdigit() for c in token)
all_upper = token.isupper()
code = " 42 "
clean = code.strip()
numeric = clean.isdigit()
value = int(clean) if numeric else 0
folded = "STRASSE".casefold()
padded = token.rjust(10, ".")
How it works
isalnum,isdigit,isupperclassify content.stripthenisdigitguards anintconversion.casefoldandrjustnormalize and pad.
Keywords and builtins used here
anyelseforifint
The run, in numbers
- Lines
- 13
- Characters to type
- 278
- Tokens
- 83
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 55 seconds.
Step 5 of 9 in Strings, step 11 of 72 in Language basics.