如何在 Python 中格式化具有可变位数的数字?

发布于 2024-09-09 05:56:55 字数 234 浏览 8 评论 0原文

假设我想显示数字 123,并在前面添加可变数量的填充零。

例如,如果我想以 5 位数字显示它,我将有 digits = 5 给我:

00123

如果我想以 6 位数字显示它,我将有 digits = 6给予:

000123

我将如何在Python中做到这一点?

Say I wanted to display the number 123 with a variable number of padded zeroes on the front.

For example, if I wanted to display it in 5 digits I would have digits = 5 giving me:

00123

If I wanted to display it in 6 digits I would have digits = 6 giving:

000123

How would I do this in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

坠似风落 2024-09-16 05:56:55

如果您在格式化字符串中使用 format() 方法,该方法优于旧样式 ''% 格式,

>>> 'One hundred and twenty three with three leading zeros {0:06}.'.format(123)
'One hundred and twenty three with three leading zeros 000123.'

请参阅
http://docs.python.org/library/stdtypes.html#str.format
http://docs.python.org/library/string.html#formatstrings

这是一个可变宽度的示例

>>> '{num:0{width}}'.format(num=123, width=6)
'000123'

您甚至可以将填充字符指定为变量

>>> '{num:{fill}{width}}'.format(num=123, fill='0', width=6)
'000123'

If you are using it in a formatted string with the format() method which is preferred over the older style ''% formatting

>>> 'One hundred and twenty three with three leading zeros {0:06}.'.format(123)
'One hundred and twenty three with three leading zeros 000123.'

See
http://docs.python.org/library/stdtypes.html#str.format
http://docs.python.org/library/string.html#formatstrings

Here is an example with variable width

>>> '{num:0{width}}'.format(num=123, width=6)
'000123'

You can even specify the fill char as a variable

>>> '{num:{fill}{width}}'.format(num=123, fill='0', width=6)
'000123'
有木有妳兜一样 2024-09-16 05:56:55

有一个名为 zfill 的字符串方法:

>>> '12344'.zfill(10)
0000012344

它将用零填充字符串的左侧,以使字符串长度为 N(在本例中为 10)。

There is a string method called zfill:

>>> '12344'.zfill(10)
0000012344

It will pad the left side of the string with zeros to make the string length N (10 in this case).

十二 2024-09-16 05:56:55

随着引入格式化字符串文字(“f-strings”简而言之)在 Python 3.6 中,现在可以使用更简短的语法访问先前定义的变量:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

John La Rooy 给出的示例可以写为

In [1]: num=123
   ...: fill='0'
   ...: width=6
   ...: f'{num:{fill}{width}}'

Out[1]: '000123'

With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is now possible to access previously defined variables with a briefer syntax:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'

The examples given by John La Rooy can be written as

In [1]: num=123
   ...: fill='0'
   ...: width=6
   ...: f'{num:{fill}{width}}'

Out[1]: '000123'
鸠魁 2024-09-16 05:56:55
'%0*d' % (5, 123)
'%0*d' % (5, 123)
桜花祭 2024-09-16 05:56:55

对于那些想要使用 python 3.6+ 和 f-Strings 做同样事情的人来说,这是解决方案。

width = 20
py, vg = "Python", "Very Good"
print(f"{py:>{width}s} : {vg:>{width}s}")

For those who want to do the same thing with python 3.6+ and f-Strings this is the solution.

width = 20
py, vg = "Python", "Very Good"
print(f"{py:>{width}s} : {vg:>{width}s}")
云淡月浅 2024-09-16 05:56:55
print "%03d" % (43)

印刷

043

print "%03d" % (43)

Prints

043

难忘№最初的完美 2024-09-16 05:56:55

使用字符串格式

print '%(#)03d' % {'#': 2}
002
print '%(#)06d' % {'#': 123}
000123

更多信息:链接文本

Use string formatting

print '%(#)03d' % {'#': 2}
002
print '%(#)06d' % {'#': 123}
000123

More info here: link text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文