如何在Python中用零填充数字字符串? [英] How to pad a numeric string with zeros to the right in Python?

查看:46
本文介绍了如何在Python中用零填充数字字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 字符串有一个名为 zfill 的方法,它允许在数字字符串的左边填充零.

Python strings have a method called zfill that allows to pad a numeric string with zeros to the left.

In : str(190).zfill(8)
Out: '00000190'

我怎样才能让垫子在右边?

How can I make the pad to be on the right ?

推荐答案

参见 格式规范迷你语言:

In [1]: '{:<08d}'.format(190)
Out[1]: '19000000'

In [2]: '{:>08d}'.format(190)
Out[2]: '00000190'

这篇关于如何在Python中用零填充数字字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆