Search code examples
pythonstring

error message using multiple %s string substitutions


I haven't been able to find another example of string substitution like in this error message where %s%s is doubled up as it is here: https://github.com/django/django/blob/387475c5b2f1aa32103dbe21cb281d3b35165a0c/django/contrib/gis/utils/layermapping.py#L260

The %s%s and subsequent single %s are resulting in different values when I run code that raises that error. Looking for an explanation of the double usage.

here is the code linked:

raise LayerMapError(
    "Invalid mapping geometry; model has %s%s, "
    "layer geometry type is %s."
    % (fld_name, "(dim=3)" if coord_dim == 3 else "", ltype)
                    )

Solution

  • There's nothing special about a so called "double" %s. You simply have a string with three %s substitutions (note that it's a multiline string, although that's inconsequential) and three values to substitute:

    1. fld_name
    2. The expression "(dim=3)" if coord_dim == 3 else ""
    3. ltype

    The fact that there's no space (or anything else) between the first two %s doesn't change that.