Padding a swift String for printing
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Padding a string is a simple way to align columns in console output, logs, or generated text reports. Swift gives you a built-in method for appending padding, and it is easy to add small helper functions when you also need left padding or centered output.
Use padding for Right Padding
Swift's built-in padding(toLength:withPad:startingAt:) appends characters until a string reaches a target length. It is the easiest tool when you want to left-align text and extend it to the right.
This prints a string that fills ten characters, so the closing delimiter lines up with other padded values.
You can also use a different pad string:
That produces 420000, which is useful in some formatting tasks but not for numeric left padding. For that, a custom helper is clearer.
Write a Helper for Left Padding
Left padding means adding characters before the content. A common example is right-aligning numbers in a fixed-width column.
This produces:
Build Neater Printed Tables
Once you have right and left padding helpers, console tables become much easier to read.
The output is predictable and easier to scan than raw string concatenation with random spaces.
Understand What the Built-In Method Does
One subtle point about padding(toLength:withPad:startingAt:) is that it does not only pad. If the target length is shorter than the original string, it truncates the result. That behavior is sometimes helpful, but it can also be surprising.
This prints only the first five characters. If truncation is not what you want, wrap the built-in method in your own helper that returns the original string when it is already long enough.
Common Pitfalls
The most common mistake is expecting padding(toLength:withPad:startingAt:) to left-pad. It does not. It appends padding to the end unless you create the left padding yourself.
Another issue is forgetting that the built-in method can truncate longer strings. That may silently chop off important content in reports or logs if you do not guard against it.
Unicode can also complicate visual alignment. Swift counts user-perceived characters, which is usually correct for string logic, but console display width is not always identical for emoji or some East Asian characters. For basic ASCII-style tables this is usually fine, but test your actual output if alignment matters.
Finally, avoid scattering magic numbers through the code. Put column widths in constants so the format is easy to adjust later.
Summary
- Use
padding(toLength:withPad:startingAt:)for quick right padding. - Write a small helper with
String(repeating:)when you need left padding. - Combine padding helpers to create readable console tables and reports.
- Remember that the built-in padding method can truncate longer strings.
- Test formatting with realistic data, especially when Unicode display width matters.

