7.2 Advanced string formatting¶
Field width¶
Ex:
- A format specification may include a field width that defines the minimum number of characters that must be inserted into the string.
- If the replacement value is smaller in size than the given field width, then the string's left side is padded with space characters.
- A field width is defined in a format specification by including an integer after the colon, as in {name:16} to specify a width of 16 characters.
- Numbers will be right-aligned within the width by default, whereas most other types like strings will be left-aligned.
Aligning text¶
- A format specification can include an alignment character that determines how a value should be aligned within the width of the field.
- Alignment is set in a format specification by adding a special character before the field width integer
- The basic set of possible alignment options include left-aligned '<', right-aligned '>' and centered '^'.
Fill¶
- The fill character is used to pad a replacement field when the string being inserted is smaller than the field width.
- The default fill character is an empty space ' '
- A programmer may define a different fill character in a format specification by placing the different fill character before the alignment character.
- Ex: {score:0>4} generates "0009" if score is 9 or "0250" if score is 250
Floating-point precision¶
- '{:.1f}'.format(1.725) indicates a precision of 1, thus the resulting string would be '1.7'.