Skip to content

Commit cf19cc3

Browse files
bpo-27772: Make preceding width with 0 valid in string format. (pythonGH-11270)
Previously it was an error with confusing error message.
1 parent 8dfe156 commit cf19cc3

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Doc/library/string.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ The meaning of the various alignment options is as follows:
347347
| ``'='`` | Forces the padding to be placed after the sign (if any) |
348348
| | but before the digits. This is used for printing fields |
349349
| | in the form '+000000120'. This alignment option is only |
350-
| | valid for numeric types. It becomes the default when '0'|
351-
| | immediately precedes the field width. |
350+
| | valid for numeric types. It becomes the default for |
351+
| | numbers when '0' immediately precedes the field width. |
352352
+---------+----------------------------------------------------------+
353353
| ``'^'`` | Forces the field to be centered within the available |
354354
| | space. |
@@ -424,6 +424,10 @@ When no explicit alignment is given, preceding the *width* field by a zero
424424
sign-aware zero-padding for numeric types. This is equivalent to a *fill*
425425
character of ``'0'`` with an *alignment* type of ``'='``.
426426

427+
.. versionchanged:: 3.10
428+
Preceding the *width* field by ``'0'`` no longer affects the default
429+
alignment for strings.
430+
427431
The *precision* is a decimal number indicating how many digits should be
428432
displayed after the decimal point for a floating point value formatted with
429433
``'f'`` and ``'F'``, or before and after the decimal point for a floating point

Lib/test/test_unicode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,12 @@ def __repr__(self):
10981098
self.assertEqual('{0:^8s}'.format('result'), ' result ')
10991099
self.assertEqual('{0:^9s}'.format('result'), ' result ')
11001100
self.assertEqual('{0:^10s}'.format('result'), ' result ')
1101+
self.assertEqual('{0:8s}'.format('result'), 'result ')
1102+
self.assertEqual('{0:0s}'.format('result'), 'result')
1103+
self.assertEqual('{0:08s}'.format('result'), 'result00')
1104+
self.assertEqual('{0:<08s}'.format('result'), 'result00')
1105+
self.assertEqual('{0:>08s}'.format('result'), '00result')
1106+
self.assertEqual('{0:^08s}'.format('result'), '0result0')
11011107
self.assertEqual('{0:10000}'.format('a'), 'a' + ' ' * 9999)
11021108
self.assertEqual('{0:10000}'.format(''), ' ' * 10000)
11031109
self.assertEqual('{0:10000000}'.format(''), ' ' * 10000000)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In string formatting, preceding the *width* field by ``'0'`` no longer
2+
affects the default alignment for strings.

Python/formatter_unicode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
219219
/* The special case for 0-padding (backwards compat) */
220220
if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
221221
format->fill_char = '0';
222-
if (!align_specified) {
222+
if (!align_specified && default_align == '>') {
223223
format->align = '=';
224224
}
225225
++pos;

0 commit comments

Comments
 (0)