Split a colon-delimited local setting once
Also published in our Blogger archive.
A colon-delimited setting should usually be separated at its first delimiter, not at every colon in the value. The literal setting in this example is endpoint:https://example.test:8443/api. Calling split(":", 1) produces exactly two strings: endpoint and https://example.test:8443/api. The delimiter is removed from the returned fields. The maximum-split argument of 1 preserves both the scheme colon and port colon in the value. The assertion checks the pair before a formatted line presents the concrete result.
str.split(sep, maxsplit) is a standard Python string method. With an explicit separator, it divides the string at that delimiter; consecutive separators can produce empty fields. Here, the first colon is the divider and the rest of the value remains opaque. The official built-in types documentation for str.split specifies that at most maxsplit splits occur. This API is available in Python 3 and has no newer-version requirement.
This pattern is appropriate only when one colon is guaranteed to separate a nonempty key from an opaque remainder. It does not itself validate or handle a missing delimiter or an empty value. For example, "endpoint".split(":", 1) returns one part, while "endpoint:".split(":", 1) returns two parts and the second is empty; unpacking the former into two variables raises ValueError. If configuration values can include quoted delimiters or structured data, use that format’s parser instead. The assertion confirms this one well-formed sample rather than establishing input validation.
AI-assistance disclosure: this article was drafted with AI assistance and checked using a synthetic example.
setting = "endpoint:https://example.test:8443/api"
key, value = setting.split(":", 1)
assert (key, value) == ("endpoint", "https://example.test:8443/api")
print(f"{key} => {value}")
endpoint => https://example.test:8443/api