如何在字符串前补零?

2144

如何在数字字符串左侧填充零,以使字符串具有特定的长度?

19个回答

3347

填充字符串:

>>> n = '4'
>>> print(n.zfill(3))
004

填充数字:

>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
004
>>> print('%03d' % n)
004
>>> print(format(n, '03')) # python >= 2.6
004
>>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
004
>>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
004
>>> print('{:03d}'.format(n))  # python >= 2.7 + python3
004

字符串格式化文档


9
“Comments python >= 2.6 are incorrect. That syntax doesn't work on python >= 3. You could change it to python < 3, but may I suggest instead always using parenthesis and omitting the comments altogether (encouraging recommended usage)?”该注释“python >= 2.6”的语法在Python版本大于等于3时是不正确的。您可以将其更改为“python < 3”,但我建议始终使用括号并完全省略注释(鼓励推荐用法)。 - Jason R. Coombs
5
请注意,您不需要为格式化字符串进行编号:'{:03d} {:03d}'.format(1, 2) 会按顺序隐式分配值。 - Dragon
1
@JasonR.Coombs:我猜你是指在Python 3上应该使用print函数而不是语句吧?我已经加入了括号;由于只有一个东西被打印,现在在Py2和Py3上都可以正常工作。 - ShadowRanger
4
这些方法中是否有任何一种可以适用于处理数量不定的零? - Anders Rabo Thorbeck
11
你为什么在例子中没有使用数字7呢?请问! - Motti
显示剩余3条评论

482

只需使用字符串对象的rjust()方法。

此示例创建一个长度为10个字符的字符串,并根据需要填充:

>>> s = 'test'
>>> s.rjust(10, '0')
>>> '000000test'

在我看来,应该是“t = t.rjust(10, '0')”,否则t的值将保持不变(至少对我来说是这样)。 - Stanislav Koncebovski
3
@StanislavKoncebovski 在Python中,字符串是不可变的。无论你对它做什么,字符串的值始终不会改变,如果你想更新变量以引用新的字符串,你总是需要重新分配。这与rjust无关。 - Neil
以前从未听说过这种方法 - 擅长隐藏,但现在已经被“揭露”了! - WestCoastProjects
@Paul D.Eden,你在理论上可能是正确的,但我再次检查了一下,如果你不像t = t.rjust(10, '0')这样分配,你将无法在t中获得'000000test'。我的断言是基于一个测试。我正在使用Python 3.7。 - Stanislav Koncebovski

216

除了使用zfill,您还可以使用通用字符串格式化:

print(f'{number:05d}') # (since Python 3.6), or
print('{:05d}'.format(number)) # or
print('{0:05d}'.format(number)) # or (explicit 0th positional arg. selection)
print('{n:05d}'.format(n=number)) # or (explicit `n` keyword arg. selection)
print(format(number, '05d'))

关于字符串格式化f-字符串的文档。


@Konrad:“然而,文档说要使用format。”我知道我来晚了,但我想看看你是什么意思。我看到的文档(https://docs.python.org/3/library/stdtypes.html#old-string-formatting)说使用`format`或其他替代方案“可能有助于避免与%插值相关的[前述]错误”。这不是非常健壮的“弃用”。 - LarsH
1
@LarsH 嗯,值得注意的是,我回答中的链接最初指向“%”格式,现在则指向 str.format 格式。我没有改变链接!相反,链接背后的Python文档网站被重写了。除此之外,文档曾经有更强烈的措辞,并且明确指出str.format “应该优先选择使用”,就像我在引用的评论中所写的一样。 - Konrad Rudolph

204

对于使用Python 3.6+的f-strings

>>> i = 1
>>> f"{i:0>2}"  # Works for both numbers and strings.
'01'
>>> f"{i:02}"  # Works only for numbers.
'01'

对于 Python 2.6 到 Python 3.5:

>>> "{:0>2}".format("1")  # Works for both numbers and strings.
'01'
>>> "{:02}".format(1)  # Works only for numbers.
'01'

那些标准格式说明符[[填充]对齐][最小宽度][0][最小宽度]


谢谢。我们在哪里可以找到类似 f"{i:0>2}" 这样的官方文档?我能找到的唯一关于 f-string 的官方文档是 https://peps.python.org/pep-0498/#format-specifiers,但当我输入“pad”或“fill”时,使用 ctrl-f 没有结果。 - Good Pen
@GoodPen 很不幸,Python文档从未达到PHP文档的易用性,但我在https://peps.python.org/pep-3101/#standard-format-specifiers找到了它。 - Cees Timmerman
现在我通过 pydoc FORMATING 找到了它。 - Good Pen

75
>>> '99'.zfill(5)
'00099'
>>> '99'.rjust(5,'0')
'00099'

如果您想要相反的结果:

>>> '99'.ljust(5,'0')
'99000'

45

str(n).zfill(width) 可以用于 stringintfloat,适用于 Python 2.x 和 3.x:

>>> n = 3
>>> str(n).zfill(5)
'00003'
>>> n = '3'
>>> str(n).zfill(5)
'00003'
>>> n = '3.0'
>>> str(n).zfill(5)
'003.0'

34

如何以最符合Python风格的方式在数字字符串左侧填充零,使其具有特定长度?

str.zfill是专门用来实现这一功能的:

>>> '1'.zfill(4)
'0001'

请注意,它专门处理请求的数字字符串,并将 +- 移动到字符串的开头:

>>> '+1'.zfill(4)
'+001'
>>> '-1'.zfill(4)
'-001'

这里是关于 str.zfill 的帮助文档:

>>> help(str.zfill)
Help on method_descriptor:

zfill(...)
    S.zfill(width) -> str

    Pad a numeric string S with zeros on the left, to fill a field
    of the specified width. The string S is never truncated.

性能

这也是替代方法中最高效的一种:

>>> min(timeit.repeat(lambda: '1'.zfill(4)))
0.18824880896136165
>>> min(timeit.repeat(lambda: '1'.rjust(4, '0')))
0.2104538488201797
>>> min(timeit.repeat(lambda: f'{1:04}'))
0.32585487607866526
>>> min(timeit.repeat(lambda: '{:04}'.format(1)))
0.34988890308886766

为了最好地将苹果与苹果进行比较,使用% 方法(请注意,它实际上比较慢),否则将进行预计算:

>>> min(timeit.repeat(lambda: '1'.zfill(0 or 4)))
0.19728074967861176
>>> min(timeit.repeat(lambda: '%04d' % (0 or 1)))
0.2347015216946602

实现

经过一番搜索,我找到了zfill方法的实现,它在Objects/stringlib/transmogrify.h中:

static PyObject *
stringlib_zfill(PyObject *self, PyObject *args)
{
    Py_ssize_t fill;
    PyObject *s;
    char *p;
    Py_ssize_t width;

    if (!PyArg_ParseTuple(args, "n:zfill", &width))
        return NULL;

    if (STRINGLIB_LEN(self) >= width) {
        return return_self(self);
    }

    fill = width - STRINGLIB_LEN(self);

    s = pad(self, fill, 0, '0');

    if (s == NULL)
        return NULL;

    p = STRINGLIB_STR(s);
    if (p[fill] == '+' || p[fill] == '-') {
        /* move sign to beginning of string */
        p[0] = p[fill];
        p[fill] = '0';
    }

    return s;
}

让我们来看一下这段C代码。

它首先按位置解析参数,这意味着它不允许关键字参数:

>>> '1'.zfill(width=4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zfill() takes no keyword arguments

然后它会检查字符串是否与指定长度相同或更长,如果是,则返回该字符串。

>>> '1'.zfill(0)
'1'

zfill 调用 pad(这个 pad 函数也被 ljustrjustcenter 调用)。它基本上将内容复制到一个新字符串中,并填充填充物。

static inline PyObject *
pad(PyObject *self, Py_ssize_t left, Py_ssize_t right, char fill)
{
    PyObject *u;

    if (left < 0)
        left = 0;
    if (right < 0)
        right = 0;

    if (left == 0 && right == 0) {
        return return_self(self);
    }

    u = STRINGLIB_NEW(NULL, left + STRINGLIB_LEN(self) + right);
    if (u) {
        if (left)
            memset(STRINGLIB_STR(u), fill, left);
        memcpy(STRINGLIB_STR(u) + left,
               STRINGLIB_STR(self),
               STRINGLIB_LEN(self));
        if (right)
            memset(STRINGLIB_STR(u) + left + STRINGLIB_LEN(self),
                   fill, right);
    }

    return u;
}
在调用pad后,zfill会将任何原本位于字符串开头的+-移动到字符串开头。
请注意,原始字符串实际上不需要是数字:
>>> '+foo'.zfill(10)
'+000000foo'
>>> '-foo'.zfill(10)
'-000000foo'

对于性能,是否存在f字符串更好的情况,包括Python2与Python3的用例?另外,我认为由于zfill不常见,将文档链接放在回答中可能会有所帮助。 - elad silver
1
@eladsilver 取决于您的意图,记住 +- 的行为,并且我添加了文档链接! - Russia Must Remove Putin

32

对于那些来这里是为了理解而不仅仅是快速答案的人,我特别为时间字符串做了以下内容:

hour = 4
minute = 3
"{:0>2}:{:0>2}".format(hour,minute)
# prints 04:03

"{:0>3}:{:0>5}".format(hour,minute)
# prints '004:00003'

"{:0<3}:{:0<5}".format(hour,minute)
# prints '400:30000'

"{:$<3}:{:#<5}".format(hour,minute)
# prints '4$$:3####'

"0"代表要用两个填充字符"0"替换的内容,如果没有指定,默认为空格。

">"符号将所有两个"0"字符对齐到字符串左侧。

":"符号是格式规范。


30

如果使用Python >= 3.6,最干净的方法是使用f-strings字符串格式化

>>> s = f"{1:08}"  # inline with int
>>> s
'00000001'
>>> s = f"{'1':0>8}"  # inline with str
>>> s
'00000001'
>>> n = 1
>>> s = f"{n:08}"  # int variable
>>> s
'00000001'
>>> c = "1"
>>> s = f"{c:0>8}"  # str variable
>>> s
'00000001'

我希望使用 int 进行格式化,因为只有这样符号才能被正确处理:

>>> f"{-1:08}"
'-0000001'

>>> f"{1:+08}"
'+0000001'

>>> f"{'-1':0>8}"
'000000-1'

23

对于数字:

i = 12
print(f"{i:05d}")

输出

00012

1
我更希望你能得到“00002”。同时,也许一个i>10的例子会是个好主意。尽管如此,这对我有所帮助。 - André Aichert
我根据你的建议更新了我的答案。谢谢! - user1315621

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接