Displaying Output with cat() and print()
Distinguish between cat() for console output and print() for objects.
Displaying Output with cat() and print() is a free R Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
print() — The Default Output Function
print() is R's default method for displaying objects. It uses the object's class to dispatch the right formatting method. In interactive sessions, typing a variable name is equivalent to calling print() on it.
x <- c(1, 2, 3, 4, 5)
print(x)
# Equivalent to:
xcat() — Raw Character Output
cat() concatenates and outputs its arguments as raw text, with no surrounding quotes, no index markers like [1], and no newline at the end by default. It is designed for formatted text output, not for displaying R objects.
name <- 'Alice'
age <- 30
cat('Name:', name, '\n')
cat('Age:', age, '\n')
cat('Score:', 92.5, '\n')Key Difference: Quotes and [1]
The most visible difference is how character strings are displayed. print() wraps strings in quotes and adds [1]; cat() outputs the raw characters without any decoration.
msg <- 'Hello World'
print(msg)
cat(msg, '\n')cat() with sep Argument
cat() accepts a sep argument that is placed between each output item. The default is a single space. Use sep = '' for no separator, or any other string to format output precisely.
cat('a', 'b', 'c', sep = '')
cat('a', 'b', 'c', sep = '-')
cat(1:5, sep = ', ')
cat('x =', 42, sep = '')cat() with fill Argument
The fill argument adds a newline when the output exceeds a given width (in characters). Setting fill = TRUE uses the current width option. This is handy for wrapping long word sequences.
words <- c('The', 'quick', 'brown', 'fox', 'jumps',
'over', 'the', 'lazy', 'dog')
cat(words, fill = 30)print() Method Dispatch
print() is a generic function — it dispatches to a class-specific method. For example, print.data.frame formats data frames nicely, print.factor shows levels, etc. This is why the same call looks different for different objects.
# print dispatches differently per class
print(42L) # integer
print(factor(c('a', 'b', 'a'))) # factor with levels
print(list(x = 1, y = 'hi')) # listinvisible() — Suppress Auto-Print
invisible(x) returns its argument but suppresses the automatic printing when a value is returned from a function or expression. It is used in functions that return values silently, like assignment operators.
# Explicit print shows the value
f <- function(x) invisible(x * 2)
result <- f(5)
# Nothing printed automatically
f(5)
# But print() forces it
print(f(5))message() — To stderr
message() writes to stderr rather than stdout. It automatically appends a newline. Unlike cat() or print(), messages can be suppressed with suppressMessages() and caught with tryCatch().
message('This goes to stderr')
cat('This goes to stdout\n')
# Suppress messages:
suppressMessages(message('hidden'))
cat('Still visible\n')When to Use cat() vs print()
Use cat() when you want to produce human-readable text output — log lines, progress messages, formatted reports. Use print() when you want to inspect R objects faithfully, preserving their type information and structure.
# cat() for formatted output
cat(sprintf('Processing %d records...\n', 500))
cat('Done.\n')
# print() to inspect objects
result <- list(n = 500, mean = 4.2, sd = 1.1)
print(result)print() on Data Frames
print.data.frame formats data frames with row numbers, column headers, and aligned values. You can control how many rows are shown with print(df, n = k) style or by using head() before printing.
df <- data.frame(
name = c('Alice', 'Bob', 'Carol'),
score = c(88, 95, 72)
)
print(df)cat() for Loop Progress
A practical use of cat() is printing loop progress. Because cat() does not add automatic newlines, you can build up a line incrementally, then flush it with '\n'.
for (i in 1:5) {
cat(sprintf('Processing file %d of 5...\n', i))
}
cat('All files processed.\n')Quick Check
Which output function writes to stderr, automatically appends a newline, and can be suppressed with suppressMessages()?
Output Functions: Key Takeaways
Key takeaways for R output functions:
print(x)— inspect R objects; uses class-specific formatting; adds[1]and quotescat(...)— raw text output; no quotes; no automatic newline; use'\n'explicitlycat()acceptssepandfillfor formatting controlinvisible(x)— return x without auto-printingmessage()— goes to stderr; auto-newline; suppressed bysuppressMessages()- Use
cat()/sprintf()together for formatted reports and logs
x <- 'result'
print(x) # shows: [1] "result"
cat(x, '\n') # shows: result
message('info: ', x) # goes to stderr
f <- function() invisible(42)
f() # nothing printed
print(f()) # forces print: [1] 42Frequently asked questions
Is the “Displaying Output with cat() and print()” lesson free?
Yes — the full text of “Displaying Output with cat() and print()” is free to read here on the web, and the R Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the R Academy course, upgrade to CoddyKit PRO.
What will I learn in “Displaying Output with cat() and print()”?
Distinguish between cat() for console output and print() for objects. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start R Academy?
No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Displaying Output with cat() and print()” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this R Academy lesson?
Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building Strings with paste() and paste0()
- Formatted Output with sprintf()
- Displaying Output with cat() and print()
- String Padding and Alignment