liveBook · Manning

Lpad

As explained in Julia as a Second Language

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

lpad left-pads text until it reaches a specified width. It fills the extra space with a chosen character, or with spaces when no character is provided. This makes numeric values and other fields line up cleanly in terminal tables. section 5.1.4 section 5.5

Use lpad to control the visible width of a text field:

  • lpad("ABC", 6, '-') returns "---ABC". section 5.1.4
  • lpad("16.75", 5) preserves the value while making it suitable for a five-character column; spaces are added only when needed. section 5.1.4
  • Pair lpad with rpad when labels and text should align from opposite sides. section 5.1.3

Aligning pizza prices

Inside a pizza-table loop, use lpad(price(pz), 5) for the price column while rpad(name(pz), 12) and rpad(portion(pz), 4) align the other fields. Values such as 10.5 and 20.75 then occupy a consistent five-character column. section 5.1 section 5.1.4

julia> lpad("ABC", 6, '-')       #1
"---ABC"
julia> rpad("ABC", 6, '-')       #2
"ABC---"

Compare left and right padding directly with runnable REPL expressions. section 5.1.4

How Julia as a Second Language covers it

How deep it goes
Explains target widths, custom characters, default spaces, and runnable table examples without covering implementation internals.
Examples
Demonstrates custom hyphen padding, string-width checks, and aligned pizza sales tables.
What it assumes
Assumes basic strings and output functions while teaching padding and column-width selection through concrete examples.
What you can do after
Apply via Julia to build consistently aligned text columns in terminal or file output.
How it connects
Connects lpad with rpad, print, println, string length, and column alignment.

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.