Python String | format method
Start your free 7-days trial now!
Python's str.format(~)
method returns a formatted string with provided argument values inserted in place of placeholders within the source string.
Parameters
2 types of parameters:
Type | Description |
---|---|
Positional | Specify the position of the element to replace |
Keyword | Specify key = value for replacing |
Return value
Returns the formatted string.
Examples
Positional parameter
To specify placeholders {}
with the numeric index of positional arguments to format the string with:
x = 'Meet {0}. {0} is {1} years old.'x.format("Sky", 24)
'Meet Sky. Sky is 24 years old.'
"Sky"
is provided as first argument (i.e.index position 0
), hence it is inserted at occurrences of {0}
in the source string. As 24
is provided as the second argument (i.e. index position 1
), it is inserted at {1}
in the source string.
Keyword parameter
To insert "Sky"
, "Towner"
and 24
in place of the {fname:<}
, {lname}
and {age}
placeholders respectively:
y = 'My name is {fname:<} {lname}. I am {age}.'y.format(fname="Sky", lname="Towner", age=24)
'My name is Sky Towner. I am 24.'
Note that we provide a formatting type :<
for {fname}
which will left-align the string within the available space.
Formatting output
To format the value provided as population with thousands separator:
z = 'The population of this town is {population:,}.'z.format(population=1200000))
The population of this town is 1,200,000.
By providing :,
formatting type, the population value 1200000
is formatted with comma thousands separator in the return string.
List of formatting types
Alignment options
Option | Meaning |
---|---|
| Forces the field to be left-aligned within the available space. |
| Forces the field to be right-aligned within the available space. |
| Forces the padding to be placed after the sign (if any) but before the digits. |
| Forces the field to be centered within the available space. |
Sign options
Option | Meaning |
---|---|
| Indicates that a sign should be used for both positive as well as negative numbers. |
| Indicates that a sign should be used only for negative numbers (this is the default behavior). |
| Indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers. |
String presentation
Type | Meaning |
---|---|
| String format. This is the default type for strings and may be omitted. |
| The same as |
Integer presentation
Type | Meaning |
---|---|
| Binary format. Outputs the number in base 2. |
| Character. Converts the integer to the corresponding unicode character before printing. |
| Decimal Integer. Outputs the number in base 10. |
| Octal format. Outputs the number in base 8. |
| Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9. |
| Hex format. Outputs the number in base 16, using upper-case letters for the digits above 9. |
| Number. This is the same as |
| The same as |
Floating point and decimal presentation
Type | Meaning |
---|---|
| Exponent notation. Prints the number in scientific notation using the letter |
| Exponent notation. Same as |
| Fixed-point notation. Displays the number as a fixed-point number. The default precision is 6. |
| Fixed-point notation. Same as |
| General format. |
| General format. Same as |
| Number. This is the same as |
| Percentage. Multiplies the number by 100 and displays in fixed ( |
| Underscore as a thousands separator. |
| Comma as a thousand separator. |