Python format string function
Starting with Python 2.6, a new string formatting function, str.format(), was added, which enhances string formatting capabilities.
The basic syntax uses {} and : to replace the old %.
The format function can accept any number of arguments, and the positions do not have to be in order.
Example
"
{} {}
"
.
format
(
"
hello
"
,
"
world
"
)
Without specifying positions, use the default order
'
hello world
'
"
{0} {1}
"
.
format
(
"
hello
"
,
"
world
"
)
Specify positions
'
hello world
'
"
{1} {0} {1}
"
.
format
(
"
hello
"
,
"
world
"
)
Specify positions
'
world hello world
'
You can also set parameters:
Example
#!/usr/bin/python
-- coding: UTF-8 --
(
"
Website name: {name}, address: {url}
"
.
format
(
name
=
"
Runoob Tutorial
"
,
url
=
"
www.runoob.com
"
)
)
Set parameters through a dictionary
site
= {
"
name
"
:
"
Runoob Tutorial
"
,
"
url
"
:
"
www.runoob.com
"
}
(
"
Website name: {name}, address: {url}
"
.
format
(
**
site
)
)
Set parameters through list indices
my_list
=
[
'
Runoob Tutorial
'
,
'
www.runoob.com
'
]
(
"
Website name: {0[0]}, address: {0[1]}
"
.
format
(
my_list
)
)
"0" is required
The output is:
网站名:Runoob Tutorial, 地址 www.runoob.com
网站名:Runoob Tutorial, 地址 www.runoob.com
网站名:Runoob Tutorial, 地址 www.runoob.com
You can also pass an object to str.format():
Example
#!/usr/bin/python
-- coding: UTF-8 --
class
AssignValue
(
object
)
:
def
init
(
self
,
value
)
:
self
.
value
=
value
my_value
=
AssignValue
(
6
)
(
'
value is: {0.value}
'
.
format
(
my_value
)
)
"0" is optional
The output is:
value 为: 6
Number formatting
The following table shows various ways to format numbers with str.format():
>>> print("{:.2f}".format(3.1415926))
3.14
| Digit | Format | Output | Description |
|---|---|---|---|
| 3.1415926 | {:.2f} | 3.14 | Keep two decimal places |
| 3.1415926 | {:+.2f} | +3.14 | Keep two decimal places with a sign |
| -1 | {:-.2f} | -1.00 | Keep two decimal places with a sign |
| 2.71828 | {:.0f} | 3 | No decimals |
| 5 | {:0>2d} | 05 | Zero-pad a number (fill on the left, width 2) |
| 5 | {:x<4d} | 5xxx | Pad a number with x (fill on the right, width 4) |
| 10 | {:x<4d} | 10xx | Pad a number with x (fill on the right, width 4) |
| 1000000 | {:,} | 1,000,000 | Number format with comma separators |
| 0.25 | {:.2%} | 25.00% | Percentage format |
| 1000000000 | {:.2e} | 1.00e+09 | Exponential notation |
| 13 | {:>10d} | 13 | Right-aligned (default, width 10) |
| 13 | {:<10d} | 13 | Left-aligned (width 10) |
| 13 | {:^10d} | 13 | Center-aligned (width 10) |
| 11 |
'{:b}'.format(11)
|
'{:d}'.format(11) '{:o}'.format(11) '{:x}'.format(11) '{:#x}'.format(11) '{:#X}'.format(11) |
1011 11 13 b 0xb 0XB| base |
^, <^, < and > stand for center, left alignment, and right alignment respectively, followed by a width; the : sign is followed by the fill character, which must be a single character and defaults to a space if not specified.
- Display a + before positive numbers and a - before negative numbers; a (space) means add a space before positive numbers
b, d, o, and x stand for binary, decimal, octal, and hexadecimal respectively.
In addition, we can use curly braces {} to escape curly braces, as shown in the following example:
Example
#!/usr/bin/python
-- coding: UTF-8 --
(
"
The position corresponding to {} is {{0}}
"
.
format
(
"
runoob
"
)
)
The output is:
runoob 对应的位置是 {0}
Python Strings