print() and cat()
print and cat are two ways to show things on the screen: print shows a value neatly, while cat writes plain text like a simple message. It is the way R speaks back to you.
In this page:
Syntax
print(object)
cat("text", value, "\n")
print() and cat()
print() displays an R object using its formatted representation, including quotes around strings and type-specific formatting.
cat() concatenates and prints its arguments as raw text with no quotes, and is often used for cleaner, more readable output. Both are the primary ways to produce output in an R script.
Note:
- Use cat() when you want clean, human-readable output.
- Use print() to see an object's full, unambiguous representation.
Example: print() and cat()
print("Hello, World!")
cat("Hello, World!\n")
x <- c(1, 2, 3)
print(x)
cat("Vector:", x, "\n")
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
print("a", "b")to print two values, when the second argument is not part of the message; usecatorpaste. - Forgetting
\nincat("Hello"), so the next output stays on the same line. - Expecting
cat()to work on a list or data frame, which gives an error, whenprint()handles those.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: