python字符串補齊方法


原字符串左側對齊, 右側補零:

str.ljust(width,'0')
input: '789'.ljust(32,'0')
output: '78900000000000000000000000000000'

原字符串右側對齊, 左側補零:

方法一:

str.rjust(width,'0') 
input: '798'.rjust(32,'0')
output: '00000000000000000000000000000798'

方法二:

str.zfill(width)
input: '123'.zfill(32)
output:'00000000000000000000000000000123'

方法三:

'%07d' % n
input: '%032d' % 89
output:'00000000000000000000000000000089'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM