Previously it was an error with confusing error message.
| ``'='`` | Forces the padding to be placed after the sign (if any) |
| | but before the digits. This is used for printing fields |
| | in the form '+000000120'. This alignment option is only |
- | | valid for numeric types. It becomes the default when '0'|
- | | immediately precedes the field width. |
+ | | valid for numeric types. It becomes the default for |
+ | | numbers when '0' immediately precedes the field width. |
+---------+----------------------------------------------------------+
| ``'^'`` | Forces the field to be centered within the available |
| | space. |
sign-aware zero-padding for numeric types. This is equivalent to a *fill*
character of ``'0'`` with an *alignment* type of ``'='``.
+.. versionchanged:: 3.10
+ Preceding the *width* field by ``'0'`` no longer affects the default
+ alignment for strings.
+
The *precision* is a decimal number indicating how many digits should be
displayed after the decimal point for a floating point value formatted with
``'f'`` and ``'F'``, or before and after the decimal point for a floating point
self.assertEqual('{0:^8s}'.format('result'), ' result ')
self.assertEqual('{0:^9s}'.format('result'), ' result ')
self.assertEqual('{0:^10s}'.format('result'), ' result ')
+ self.assertEqual('{0:8s}'.format('result'), 'result ')
+ self.assertEqual('{0:0s}'.format('result'), 'result')
+ self.assertEqual('{0:08s}'.format('result'), 'result00')
+ self.assertEqual('{0:<08s}'.format('result'), 'result00')
+ self.assertEqual('{0:>08s}'.format('result'), '00result')
+ self.assertEqual('{0:^08s}'.format('result'), '0result0')
self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
--- /dev/null
+In string formatting, preceding the *width* field by ``'0'`` no longer
+affects the default alignment for strings.
/* The special case for 0-padding (backwards compat) */
if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
format->fill_char = '0';
- if (!align_specified) {
+ if (!align_specified && default_align == '>') {
format->align = '=';
}
++pos;