Skip to content

Exercise 5 of 10 · String formatting

Format an Order Line

What you will make

A program that builds one clean, formatted sentence out of separate pieces of text and numbers.

The one new idea: Building formatted strings with paste0()

Real programs constantly need to stitch labels, numbers, and punctuation into a single readable message — paste0() is R's tool for combining pieces into one string with full control over the spacing.

Go straight to the code ↓

The concept

When you need to build a single message out of several separate pieces — some text, some numbers — R gives you paste() and paste0(). Both combine their arguments into one string, converting numbers to text automatically along the way. The difference is spacing: paste() inserts a single space between every pair of arguments by default, while paste0() is shorthand for paste(..., sep = ""), joining everything directly with no separator at all. If your pieces already contain exactly the spaces and punctuation you want, paste0() is usually the right choice, since it won't add anything extra on top of what you wrote.

Worked example

r
city <- "Nairobi"
visitors <- 12
message <- paste0(visitors, " visitors checked in from ", city, ".")
cat(message, "\n", sep = "")
Output
12 visitors checked in from Nairobi.

Each piece here — the number 12, the phrase " visitors checked in from ", the city name, and the period — already has its spacing built in exactly where it belongs. Because paste0() doesn't insert anything extra between pieces, they join up into one clean sentence with no doubled or missing spaces.

Your turn

The starter code tries to build a similar sentence describing an order, but it uses paste() instead of paste0(). Since the pieces being combined already include their own spacing, paste()'s automatic space between every argument creates extra, unwanted spaces in the final sentence. Change the function used to build line so it stops adding that extra spacing.

If something goes wrong

If the printed line still has odd gaps or a space right before the final period, check whether you're still calling paste() rather than paste0() — they're easy to mix up since the names are so similar. If the sentence looks completely correct already, count the pieces being joined and make sure none were accidentally deleted or reordered while you were editing.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Trying to fix the spacing by removing the " " piece between quantity and item.
That piece is exactly where the space between the number and the word "notebook" comes from — removing it would produce "3notebooks" stuck together; the real fix is switching functions, not removing content.
Adding sep = "" to a paste() call instead of switching to paste0().
This actually works and is equivalent to paste0() — but it's easy to mistype sep = "" as sep = " " by accident, so paste0() is the more direct, less error-prone fix here.
Forgetting that quantity is a number, not text.
paste0() automatically converts numbers to text when combining them with strings, so quantity doesn't need any special conversion before being passed in.

Want a blank editor instead? Open the R playground.