$
and hm
means nothing special, they are just characters within you string. Simply run the string formatting code and you'll see:
>>> "%%%du|%%17$hn|" % 2493
'%2493u|%17$hn|'
In the above string %d
is getting replaced by your number 2493
and %%
is used to display %
in your string as single %
carries a special meaning in %-string formatting. Rest are normal string character.
%-formatting (also known as printf-style string formatting) is a old style of Python's string formatting which is these days is generally done by using str.format
function. PEP 3101 proposed the replacement of the %
operator with the new, advanced string formatting.
A very nice comparison between both is available at: "Python string formatting: % vs. .format"
From the "printf-style String Formatting" document, it allows following conversion types:
Conversion Meaning
'd' Signed integer decimal.
'i' Signed integer decimal.
'o' Signed octal value.
'u' Obsolete type – it is identical to 'd'.
'x' Signed hexadecimal (lowercase).
'X' Signed hexadecimal (uppercase).
'e' Floating point exponential format (lowercase).
'E' Floating point exponential format (uppercase).
'f' Floating point decimal format.
'F' Floating point decimal format.
'g' Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
'G' Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
'c' Single character (accepts integer or single character string).
'r' String (converts any Python object using repr()).
's' String (converts any Python object using str()).
'a' String (converts any Python object using ascii()).
'%' No argument is converted, results in a '%' character in the result.
and has conversion flags characters as:
+------+--------------------------------------------------------------------------------------------------------------+
| Flag | Meaning |
+------+--------------------------------------------------------------------------------------------------------------+
| '#' | The value conversion will use the “alternate form” (where defined below). |
+------+--------------------------------------------------------------------------------------------------------------+
| '0' | The conversion will be zero padded for numeric values. |
+------+--------------------------------------------------------------------------------------------------------------+
| '-' | The converted value is left adjusted (overrides the '0' conversion if both are given). |
+------+--------------------------------------------------------------------------------------------------------------+
| ' ' | (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. |
+------+--------------------------------------------------------------------------------------------------------------+
| '+' | A sign character ('+' or '-') will precede the conversion (overrides a “space” flag). |
+------+--------------------------------------------------------------------------------------------------------------+