Partition method of Python strings
The `partition` method of `str` covers a niche use case - It is useful when a string needs to be split into exactly two parts based on a separator.
The `split`
method is commonly used for splitting strings, which returns a list whose length can change based on the number of times the separator is present.
host = "localhost:8000"
print(host.split(":"))
# Output: ['localhost', '8000']
host = "localhost"
print(host.split(":"))
# Output: ['localhost']
host = ""
print(host.split(":"))
# Output: ['']
`partition`
is helpful when we need to get precisely two parts from a string using a separator. It returns a three-membered tuple containing the part before, the separator itself (if a match is found, else empty string), and the part after the separator.
server_name = "localhost:5000"
print(server_name.partition(":"))
# Output: ('localhost', ':', '5000')
server_name = "localhost"
print(server_name.partition(":"))
# Output: ('localhost', '', '')
server_name = ""
print(server_name.partition(":"))
# Output: ('', '', '')
Using `partition
` instead of `split
` saves us from additional checks on the result's length when exactly two parts are required.
Flask uses the `partition
` method to determine the host and port to use when running the Flask app. You can see the source code here.
A simplified version of the code is added below.
def run(
self,
host: str | None = None,
port: int | None = None,
debug: bool | None = None,
load_dotenv: bool = True,
**options: t.Any,
) -> None:
...
server_name = self.config.get("SERVER_NAME")
sn_host = sn_port = None
if server_name:
sn_host, _, sn_port = server_name.partition(":")
# The `partition` method will always return a three-membered tuple
if not host:
if sn_host:
host = sn_host
else:
host = "127.0.0.1"
if port or port == 0:
port = int(port)
elif sn_port:
port = int(sn_port)
else:
port = 5000
Here, the `server_name
` string is partitioned into host and port values.
I share interesting Python snippets 🐍 like this from open-source projects illustrating Python language features. Subscribe to get new posts delivered to your email 💌🚀.