liveBook · Manning

Rpad

As explained in Julia as a Second Language

rpad is a Julia function that right-pads a string to a specified length using a chosen character or a space by default.

rpad right-pads a string until it reaches a requested length. Supply a padding character such as '-' or let it default to spaces; the result is useful for readable text columns. section 5.1.4 section 5.5

Use rpad when formatting output:

  • rpad("ABC", 6, '-') returns "ABC---". section 5.1.4
  • rpad("name", 12) reserves a twelve-character name column and fills unused width with spaces. section 5.1.4
  • Combine rpad with lpad when labels align on the left but numeric values align on the right. section 5.1

Aligning a pizza table

A compact table can assign fixed widths to its headings and values: pname = rpad("name", 12), psize = rpad("size", 4), and pprice = lpad(price(pz), 5). Names and sizes then occupy consistent columns while prices line up at the right edge. section 5.1 section 5.1.4

Figure 5.2 Formatted display of pizza sales using alignment and colors in a Unix terminal window
05-02

Inspect a formatted pizza-sales display to see the practical result of aligned text columns and styled terminal output. section 5.0

How Julia as a Second Language covers it

How deep it goes
Defines target width and padding characters, demonstrates REPL calls and tables, but does not discuss implementation internals.
Examples
Uses hyphen-padded strings, pizza-sales tables, and neatly aligned trigonometric tables.
What it assumes
Assumes basic strings and output calls while teaching padding through short examples and progressively formatted tables.
What you can do after
Apply it via Julia to build readable fixed-width text tables for terminal or file output.
How it connects
Connects rpad with lpad, strings, print, println, printstyled, and formatted table output.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.