UrlString

Easily build URL strings with / syntax

UrlString is a convenience wrapper around a string object for making it easy to build URL strings. It borrows the / syntax from pathlib to allow for the building of natural looking URLs.

An example implementation by UK Trade team:

class UrlString(str):
    def __truediv__(self, other):
        base = self.strip('/')
        path = other.strip('/')
        return UrlString(f'{base}/{path}/')

Usage:

home = UrlString('https://example.com')
new_url = home / 'foo' / 'bar'
print(new_url)
https://example.com/foo/bar/