Format output string, right alignment
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Right-aligning text output is a common formatting task when building reports, printing tables, or displaying financial data in the terminal. Most programming languages provide built-in formatting tools that make right alignment straightforward. This article shows how to right-align strings and numbers in Python, Java, C++, and JavaScript, with practical examples for each.
Why Right Alignment Matters
Right alignment places content flush against the right edge of a fixed-width field, padding the left side with spaces (or another character). This is especially useful for numeric columns because it lines up digits by place value, making tables easier to scan.
Without right alignment, the digits do not line up and the table is harder to read at a glance.
Python
Python offers three approaches for right alignment: f-strings, the str.format() method, and the rjust() string method.
Using f-strings
Output:
The >10 format specifier means right-align within a 10-character field. The <10 means left-align. You can combine this with numeric formatting:
Using str.format()
Using rjust()
The rjust() method pads the left side with spaces by default. You can pass a second argument to use a different fill character.
Java
Java provides String.format() and System.out.printf(), which follow the same format specifier syntax used in C's printf.
The %10d format specifier right-aligns an integer within a 10-character field. The %-10s left-aligns a string. Adding a comma flag formats numbers with thousand separators:
C++
C++ uses <iomanip> manipulators with std::cout for output formatting.
Key points for C++ formatting:
std::setw(n)sets the field width. It only applies to the next output operation, so you must repeat it for each value.std::rightsets right alignment. It stays in effect until you change it withstd::left.std::setfill('0')changes the padding character from space to zero, useful for formatting IDs or timestamps.
JavaScript
JavaScript's padStart() method handles right alignment for string output.
For numeric formatting with locale-aware separators:
Note that padStart() works on strings, so you must convert numbers to strings before calling it.
Formatting Entire Tables
When building a complete table, compute the maximum width for each column dynamically rather than hardcoding field widths.
This approach adapts to any data and avoids truncation when values are wider than expected.
Common Pitfalls
- Hardcoding field widths that are too narrow. If a value exceeds the specified width, most languages print the full value without padding rather than truncating it, which breaks alignment for the rest of the column.
- Forgetting that
std::setw()in C++ is consumed by the next output operation. Every value in a formatted row needs its ownsetw()call. - Mixing tabs and spaces for alignment. Tab widths vary between terminals and editors, so column alignment breaks unpredictably. Use spaces only.
- Not converting numbers to strings before calling
padStart()in JavaScript. CallingpadStarton a number produces a TypeError. - Using right alignment for text columns. Names and labels are easier to read when left-aligned. Reserve right alignment for numbers and fixed-width codes.
Summary
Right alignment is handled by format specifiers (> in Python, %Nd in Java/C), stream manipulators (std::right in C++), or string methods (padStart in JavaScript, rjust in Python). Use it for numeric columns to line up digits by place value. Compute column widths dynamically when building tables, and always use spaces rather than tabs for consistent alignment across environments.

