python zfill_Python字符串zfill()

python zfill

Python String zfill()

Python String zfill()

Python字符串zfill()

Python String zfill(width) function returns a new string of specified width. The string is filled with 0 on the left side to create the specified width.

Python字符串zfill(width)函数返回指定宽度的新字符串。 该字符串在左侧用0填充以创建指定的宽度。

If the string starts with sign characters (+,-) then the padding is done after the sign. If the specified width is less than the original string, then the original string is returned.

如果字符串以符号字符(+,-)开头,则在符号之后进行填充。 如果指定的宽度小于原始字符串,则返回原始字符串。

Python字符串zfill() (Python String zfill())

Let’s look at some examples of zfill() function.

让我们看一下zfill()函数的一些示例。

s = '100'
print(s.zfill(6))

s = '+100'
print(s.zfill(6))

s = '-100'
print(s.zfill(6))

Output:

输出:

000100
+00100
-00100

Let’s see what happens when the specified width is smaller than the string length.

让我们看看当指定的宽度小于字符串长度时会发生什么。

s = '+100'
print(s.zfill(3))

Output: +100

输出: +100

This function is useful when the string is numeric in nature. However, it works with any non-numeric string too.

当字符串本质上是数字时,此功能很有用。 但是,它也适用于任何非数字字符串。

s = 'abc'
print(s.zfill(6))

s = '+abc'
print(s.zfill(6))

Output:

输出:

000abc
+00abc
GitHub Repository. GitHub存储库中签出更多Python示例。

Official Documentation: zfill()

官方文档: zfill()

翻译自: https://www.journaldev.com/24546/python-string-zfill

python zfill

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值