String Formatting
In Python, a string-formatting expression has the syntax:
format % values
where format is a plain or Unicode string containing format specifiers and values is any single object or a collection of objects in a tuple or dictionary. Python’s string-formatting operator has roughly the same set of features as the C language’s printf and operates in a similar way. Each format specifier is a substring of format that starts with a percent sign (%) and ends with one of the conversion characters shown in Table 9-1.
Table 9-1. String-formatting conversion characters
Character | Output format | Notes |
|---|---|---|
| Signed decimal integer | Value must be number. |
| Unsigned decimal integer | Value must be number. |
| Unsigned octal integer | Value must be number. |
| Unsigned hexadecimal integer (lowercase letters) | Value must be number. |
| Unsigned hexadecimal integer (uppercase letters) | Value must be number. |
| Floating-point value in exponential form (lowercase e for exponent) | Value must be number. |
| Floating-point value in exponential form (uppercase E for exponent) | Value must be number. |
| Floating-point value in decimal form | Value must be number. |
| Like |
|
| Single character | Value can be integer or single-character string. |
| String | Converts any value with |
| String | Converts any value with |
| Literal | Consumes no value. |
Between the % and the conversion character, you can specify a number of optional modifiers, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access