swapcase And istitle Validate Casing

Flip letter case in place with str.swapcase and gate display rules with istitle, isupper, and islower, which all return False for empty or digit-only strings.

PythonForm Validation

Code

pythonsnippet
17 lines
codes = ["HTTP", "12ab", "1234", ""]

print([c.isupper() for c in codes])   # True False False False
# 纯数字与空串不满足任何大小写判定,全部 False
print([c.islower() for c in codes])

# istitle 要求每个单词首字母大写
print("Spring Boot".istitle(), "Spring boot".istitle())

# swapcase 交换大小写并往返一致
toggled = "Proxy-Auth".swapcase()
print(toggled, toggled.swapcase())

# 宽松化输入前先看原始形态再决定是否归一
raw = "MiXeD CaSe"
normal = raw.lower()
print("changed:", raw != normal, normal)

Output

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

[True, False, False, False]
[False, True, False, False]
True False
pROXY-aUTH Proxy-Auth
changed: True mixed case

Inside the snippet

The file’s state is a handful of top-level bindings: codes, toggled, raw, normal.

What to watch for

  • The runtime is the only requirement — no packages, no install.

Putting it to use

Output lines are prefixed (Spring Boot, changed:), so every step’s effect shows up right where its line prints.

Interactive Tools for This Topic

FAQ

Anything to download besides the language toolchain?
Nothing beyond the toolchain you already have for this language. Its state lives in module-level bindings: `codes`, `toggled`, `raw`.