72 of 151 menu

The zfill method

The zfill method returns a string filled with zeros to a given length. The desired string length is specified by the method parameter.

Syntax

string.zfill(string length)

Example

Let's apply the zfill method to our string:

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

Result of code execution:

'000abc'

Example

Let's use the zfill method again, but in this case, we'll specify a number in the parameter that is less than the length of our string:

txt = 'abc' print(txt.zfill(2))

As you can see from the result, we got back the original string:

'abc'

See also

  • method ljust,
    which aligns and fills the text in a line to the left
  • method rjust,
    which aligns and fills the text in a line to the right
  • function len,
    which returns the length of the string
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline