typestar

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

  1. isalnum, isdigit, isupper classify content.
  2. strip then isdigit guards an int conversion.
  3. casefold and rjust normalize and pad.

Keywords and builtins used here

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.

Type this snippet

Step 5 of 9 in Strings, step 11 of 72 in Language basics.

← Previous Next →