liveBook · Manning

Printstyled

As explained in Julia as a Second Language

printstyled is a Julia function that writes text with terminal styling such as foreground or background colors and text attributes.

printstyled writes text with terminal styling, making command-line output easier to scan. It is especially useful for highlighting headings, labels, or important values in formatted displays such as pizza-sales and sine tables. section 5.0 section 5.1

Use printstyled when ordinary print or println output needs visual emphasis:

  • Pass text as one or more arguments and select a named color with the color keyword, such as color = :cyan. section 5.1.1
  • Use bold=true for bold output; available styling symbols include colors and attributes such as underline, reverse, blink, and hidden. section 5.1.1
  • Use symbols for color names, for example :cyan, rather than memorizing terminal escape sequences. section 5.1.1
  • Combine printstyled with print, println, rpad, and lpad to create aligned, readable terminal tables. section 5.0 section 5.1

Highlighting a table heading

printstyled(rpad("name", 12), color=:cyan) print(" │ ") printstyled("size", color=:cyan) print(" │ ") printstyled(rpad("price", 5), color=:cyan) println(" │") This pattern colors only the headings while ordinary print and println calls render the table structure and rows. section 5.1

Listing 5.1 Creating a pretty table of pizza sales
function print_pizzatable(pizzas)
   print("│ ")
   printstyled(rpad("name", 12), color=:cyan)
   print(" │ ")
   printstyled("size", color=:cyan)
   print(" │ ")
   printstyled(rpad("price", 5), color=:cyan)
   println(" │")

   for pz in pizzas
        print("│ ",  rpad(name(pz), 12))
        print(" │ ", rpad(portion(pz), 4), " │ ")
        println(lpad(price(pz), 5), " │")
   end
end

Inspect the complete pizza-table function to see printstyled used with padding, tuple accessors, loops, and mixed colored and uncolored output. section 5.1

How Julia as a Second Language covers it

How deep it goes
Explains the call signature, named colors, symbols, bold output, and escape-sequence motivation without descending into terminal internals.
Examples
Demonstrates cyan hello-world output and colored headings in a formatted pizza-sales table.
What it assumes
Assumes basic strings and print or println, while teaching terminal colors, escape sequences, and symbols through short examples.
What you can do after
Apply it to build readable terminal displays with named colors and optional bold styling.
How it connects
Connects terminal styling with print, println, escape sequences, symbols, and text 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.