Displaying Output with cat() and print()
Distinguish between cat() for console output and print() for objects.
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')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