Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

str.zfill

Preenche com zeros ASCII até à largura mínima width.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

s.zfill(width)

Preenche com zeros ASCII até à largura mínima width.

Um sinal ASCII + ou - inicial fica antes do preenchimento: -42 com largura 5 passa a -0042.

A largura 2 não encurta o texto 123 que já tem três carateres.

assert "-7".zfill(4) == "-007" and "−7".zfill(4) == "00−7"

Exemplos

print("-42".zfill(5))
print("123".zfill(2))

Resultado

-0042
123

A largura 2 não encurta o texto 123 que já tem três carateres.

Fontes