如何在数字字符串左侧填充零,以使字符串具有特定的长度?
填充字符串:
>>> 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
只需使用字符串对象的rjust()
方法。
此示例创建一个长度为10个字符的字符串,并根据需要填充:
>>> s = 'test'
>>> s.rjust(10, '0')
>>> '000000test'
除了使用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'))
str.format
格式。我没有改变链接!相反,链接背后的Python文档网站被重写了。除此之外,文档曾经有更强烈的措辞,并且明确指出str.format
“应该优先选择使用”,就像我在引用的评论中所写的一样。 - Konrad Rudolph对于使用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 Penpydoc FORMATING
找到了它。 - Good Pen>>> '99'.zfill(5)
'00099'
>>> '99'.rjust(5,'0')
'00099'
如果您想要相反的结果:
>>> '99'.ljust(5,'0')
'99000'
str(n).zfill(width)
可以用于 string
、int
和 float
,适用于 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'
如何以最符合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
函数也被 ljust
、rjust
和 center
调用)。它基本上将内容复制到一个新字符串中,并填充填充物。
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'
+
和 -
的行为,并且我添加了文档链接! - Russia Must Remove Putin对于那些来这里是为了理解而不仅仅是快速答案的人,我特别为时间字符串做了以下内容:
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"字符对齐到字符串左侧。
":"符号是格式规范。
如果使用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'
对于数字:
i = 12
print(f"{i:05d}")
输出
00012
python >= 2.6
are incorrect. That syntax doesn't work onpython >= 3
. You could change it topython < 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'{:03d} {:03d}'.format(1, 2)
会按顺序隐式分配值。 - Dragonprint
函数而不是语句吧?我已经加入了括号;由于只有一个东西被打印,现在在Py2和Py3上都可以正常工作。 - ShadowRanger