Programming
String Manipulation
Padding
Zeros
Duplicate Content

Left padding a String with Zeros

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Left-padding with zeros is a formatting task, not a math operation. The goal is usually to display IDs, sequence numbers, or fixed-width fields at a consistent length without changing the underlying value. The right method depends on the language and on whether you are formatting a number or an existing string.

Decide Whether You Are Formatting Text or Numbers

This distinction matters more than it seems. If the original data is a number, formatting tools are often the best choice. If the original data is already a string, string padding methods are usually clearer.

For example, the value 42 and the string "42" may look similar, but formatting libraries and padding functions treat them differently in some languages.

The design question is: do the leading zeros belong only to display, or are they part of the canonical identifier itself?

Python: Use zfill or Format Specifiers

Python has a built-in string helper made for this exact job.

python
value = "42"
print(value.zfill(6))

If you are formatting a number rather than a string, format specifiers can be cleaner.

python
number = 42
print(f"{number:06d}")
print("{:06d}".format(number))

zfill is ideal when the input is already textual. Format specifiers are ideal when the input is numeric and you want explicit numeric formatting.

JavaScript: Use padStart

Modern JavaScript makes this simple.

javascript
const value = "42";
console.log(value.padStart(6, "0"));

If the input is numeric, convert it first.

javascript
const id = 42;
console.log(String(id).padStart(6, "0"));

This is cleaner than building loops or manual concatenation. It also keeps the intent obvious to the next reader.

Java: Use String.format

In Java, String.format is the standard approach for numeric formatting.

java
int number = 42;
String padded = String.format("%06d", number);
System.out.println(padded);

If the value is already a string and may not be purely numeric, a manual or utility-library approach is safer than parsing it as an integer and reformatting it.

java
String value = "42";
String padded = "0".repeat(Math.max(0, 6 - value.length())) + value;
System.out.println(padded);

The right choice depends on whether numeric semantics matter.

Be Careful with Signs and Overlong Values

Padding is usually straightforward until edge cases show up. For example, some formatting helpers treat negative numbers specially.

python
print(str(-42).zfill(6))

That yields -00042, which is often what you want, but it is worth knowing. Also, if the value is already longer than the target width, padding methods usually leave it unchanged rather than truncating it.

That behavior is generally correct. Padding is about extending width, not clipping data.

Keep Padding at the Presentation Boundary

A common mistake is storing padded values as if they were the real domain value when the zeros only matter for display. If an order id is logically the integer 42, storing 000042 everywhere can create sorting, comparison, and validation confusion.

If the zeros are part of an externally defined identifier format, then the padded string may be the correct canonical value. Otherwise, keep the raw value internally and pad it only at presentation or export time.

Common Pitfalls

  • Treating left-padding as a numeric transformation instead of a formatting decision.
  • Parsing a string into a number and accidentally losing meaningful leading zeros.
  • Writing manual loop-based padding when the language already provides a standard helper.
  • Forgetting that negative values and overlong inputs may need explicit thought.
  • Storing padded display values as canonical data when the zeros are only presentation detail.

Summary

  • Zero-padding is usually a formatting concern, not a data-model concern.
  • Use language-native helpers such as zfill, padStart, or String.format.
  • Choose string-based or numeric formatting based on the real input type.
  • Handle edge cases such as negative numbers and long inputs intentionally.
  • Keep padding at the display boundary unless the zero-filled string is the true identifier format.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions