@@ -3057,6 +3057,197 @@ place, and instead produce new objects.
3057
3057
always produces a new object, even if no changes were made.
3058
3058
3059
3059
3060
+ .. _bytes-formatting :
3061
+
3062
+ ``printf ``-style Bytes Formatting
3063
+ ----------------------------------
3064
+
3065
+ .. index ::
3066
+ single: formatting, bytes (%)
3067
+ single: formatting, bytearray (%)
3068
+ single: interpolation, bytes (%)
3069
+ single: interpolation, bytearray (%)
3070
+ single: bytes; formatting
3071
+ single: bytearray; formatting
3072
+ single: bytes; interpolation
3073
+ single: bytearray; interpolation
3074
+ single: printf-style formatting
3075
+ single: sprintf-style formatting
3076
+ single: % formatting
3077
+ single: % interpolation
3078
+
3079
+ .. note ::
3080
+
3081
+ The formatting operations described here exhibit a variety of quirks that
3082
+ lead to a number of common errors (such as failing to display tuples and
3083
+ dictionaries correctly). If the value being printed may be a tuple or
3084
+ dictionary, wrap it in a tuple.
3085
+
3086
+ Bytes objects (``bytes ``/``bytearray ``) have one unique built-in operation:
3087
+ the ``% `` operator (modulo).
3088
+ This is also known as the bytes *formatting * or *interpolation * operator.
3089
+ Given ``format % values `` (where *format * is a bytes object), ``% `` conversion
3090
+ specifications in *format * are replaced with zero or more elements of *values *.
3091
+ The effect is similar to using the :c:func: `sprintf ` in the C language.
3092
+
3093
+ If *format * requires a single argument, *values * may be a single non-tuple
3094
+ object. [5 ]_ Otherwise, *values * must be a tuple with exactly the number of
3095
+ items specified by the format bytes object, or a single mapping object (for
3096
+ example, a dictionary).
3097
+
3098
+ A conversion specifier contains two or more characters and has the following
3099
+ components, which must occur in this order:
3100
+
3101
+ #. The ``'%' `` character, which marks the start of the specifier.
3102
+
3103
+ #. Mapping key (optional), consisting of a parenthesised sequence of characters
3104
+ (for example, ``(somename) ``).
3105
+
3106
+ #. Conversion flags (optional), which affect the result of some conversion
3107
+ types.
3108
+
3109
+ #. Minimum field width (optional). If specified as an ``'*' `` (asterisk), the
3110
+ actual width is read from the next element of the tuple in *values *, and the
3111
+ object to convert comes after the minimum field width and optional precision.
3112
+
3113
+ #. Precision (optional), given as a ``'.' `` (dot) followed by the precision. If
3114
+ specified as ``'*' `` (an asterisk), the actual precision is read from the next
3115
+ element of the tuple in *values *, and the value to convert comes after the
3116
+ precision.
3117
+
3118
+ #. Length modifier (optional).
3119
+
3120
+ #. Conversion type.
3121
+
3122
+ When the right argument is a dictionary (or other mapping type), then the
3123
+ formats in the bytes object *must * include a parenthesised mapping key into that
3124
+ dictionary inserted immediately after the ``'%' `` character. The mapping key
3125
+ selects the value to be formatted from the mapping. For example:
3126
+
3127
+ >>> print (b ' %(language)s has %(number)03d quote types.' %
3128
+ ... {b ' language' : b " Python" , b " number" : 2 })
3129
+ b'Python has 002 quote types.'
3130
+
3131
+ In this case no ``* `` specifiers may occur in a format (since they require a
3132
+ sequential parameter list).
3133
+
3134
+ The conversion flag characters are:
3135
+
3136
+ +---------+---------------------------------------------------------------------+
3137
+ | Flag | Meaning |
3138
+ +=========+=====================================================================+
3139
+ | ``'#' `` | The value conversion will use the "alternate form" (where defined |
3140
+ | | below). |
3141
+ +---------+---------------------------------------------------------------------+
3142
+ | ``'0' `` | The conversion will be zero padded for numeric values. |
3143
+ +---------+---------------------------------------------------------------------+
3144
+ | ``'-' `` | The converted value is left adjusted (overrides the ``'0' `` |
3145
+ | | conversion if both are given). |
3146
+ +---------+---------------------------------------------------------------------+
3147
+ | ``' ' `` | (a space) A blank should be left before a positive number (or empty |
3148
+ | | string) produced by a signed conversion. |
3149
+ +---------+---------------------------------------------------------------------+
3150
+ | ``'+' `` | A sign character (``'+' `` or ``'-' ``) will precede the conversion |
3151
+ | | (overrides a "space" flag). |
3152
+ +---------+---------------------------------------------------------------------+
3153
+
3154
+ A length modifier (``h ``, ``l ``, or ``L ``) may be present, but is ignored as it
3155
+ is not necessary for Python -- so e.g. ``%ld `` is identical to ``%d ``.
3156
+
3157
+ The conversion types are:
3158
+
3159
+ +------------+-----------------------------------------------------+-------+
3160
+ | Conversion | Meaning | Notes |
3161
+ +============+=====================================================+=======+
3162
+ | ``'d' `` | Signed integer decimal. | |
3163
+ +------------+-----------------------------------------------------+-------+
3164
+ | ``'i' `` | Signed integer decimal. | |
3165
+ +------------+-----------------------------------------------------+-------+
3166
+ | ``'o' `` | Signed octal value. | \( 1) |
3167
+ +------------+-----------------------------------------------------+-------+
3168
+ | ``'u' `` | Obsolete type -- it is identical to ``'d' ``. | \( 7) |
3169
+ +------------+-----------------------------------------------------+-------+
3170
+ | ``'x' `` | Signed hexadecimal (lowercase). | \( 2) |
3171
+ +------------+-----------------------------------------------------+-------+
3172
+ | ``'X' `` | Signed hexadecimal (uppercase). | \( 2) |
3173
+ +------------+-----------------------------------------------------+-------+
3174
+ | ``'e' `` | Floating point exponential format (lowercase). | \( 3) |
3175
+ +------------+-----------------------------------------------------+-------+
3176
+ | ``'E' `` | Floating point exponential format (uppercase). | \( 3) |
3177
+ +------------+-----------------------------------------------------+-------+
3178
+ | ``'f' `` | Floating point decimal format. | \( 3) |
3179
+ +------------+-----------------------------------------------------+-------+
3180
+ | ``'F' `` | Floating point decimal format. | \( 3) |
3181
+ +------------+-----------------------------------------------------+-------+
3182
+ | ``'g' `` | Floating point format. Uses lowercase exponential | \( 4) |
3183
+ | | format if exponent is less than -4 or not less than | |
3184
+ | | precision, decimal format otherwise. | |
3185
+ +------------+-----------------------------------------------------+-------+
3186
+ | ``'G' `` | Floating point format. Uses uppercase exponential | \( 4) |
3187
+ | | format if exponent is less than -4 or not less than | |
3188
+ | | precision, decimal format otherwise. | |
3189
+ +------------+-----------------------------------------------------+-------+
3190
+ | ``'c' `` | Single byte (accepts integer or single | |
3191
+ | | byte objects). | |
3192
+ +------------+-----------------------------------------------------+-------+
3193
+ | ``'b' `` | Bytes (any object that follows the | \( 5) |
3194
+ | | :ref: `buffer protocol <bufferobjects >` or has | |
3195
+ | | :meth: `__bytes__ `). | |
3196
+ +------------+-----------------------------------------------------+-------+
3197
+ | ``'s' `` | ``'s' `` is an alias for ``'b' `` and should only | \( 6) |
3198
+ | | be used for Python2/3 code bases. | |
3199
+ +------------+-----------------------------------------------------+-------+
3200
+ | ``'a' `` | Bytes (converts any Python object using | \( 5) |
3201
+ | | ``repr(obj).encode('ascii','backslashreplace) ``). | |
3202
+ +------------+-----------------------------------------------------+-------+
3203
+ | ``'%' `` | No argument is converted, results in a ``'%' `` | |
3204
+ | | character in the result. | |
3205
+ +------------+-----------------------------------------------------+-------+
3206
+
3207
+ Notes:
3208
+
3209
+ (1)
3210
+ The alternate form causes a leading zero (``'0' ``) to be inserted between
3211
+ left-hand padding and the formatting of the number if the leading character
3212
+ of the result is not already a zero.
3213
+
3214
+ (2)
3215
+ The alternate form causes a leading ``'0x' `` or ``'0X' `` (depending on whether
3216
+ the ``'x' `` or ``'X' `` format was used) to be inserted between left-hand padding
3217
+ and the formatting of the number if the leading character of the result is not
3218
+ already a zero.
3219
+
3220
+ (3)
3221
+ The alternate form causes the result to always contain a decimal point, even if
3222
+ no digits follow it.
3223
+
3224
+ The precision determines the number of digits after the decimal point and
3225
+ defaults to 6.
3226
+
3227
+ (4)
3228
+ The alternate form causes the result to always contain a decimal point, and
3229
+ trailing zeroes are not removed as they would otherwise be.
3230
+
3231
+ The precision determines the number of significant digits before and after the
3232
+ decimal point and defaults to 6.
3233
+
3234
+ (5)
3235
+ If precision is ``N ``, the output is truncated to ``N `` characters.
3236
+
3237
+ (6)
3238
+ ``b'%s' `` is deprecated, but will not be removed during the 3.x series.
3239
+
3240
+ (7)
3241
+ See :pep: `237 `.
3242
+
3243
+ .. note ::
3244
+
3245
+ The bytearray version of this method does *not * operate in place - it
3246
+ always produces a new object, even if no changes were made.
3247
+
3248
+ .. seealso :: :pep:`461`.
3249
+ .. versionadded :: 3.5
3250
+
3060
3251
.. _typememoryview :
3061
3252
3062
3253
Memory Views
0 commit comments