← Back to R Course | Chapter 6: Strings | Lesson 1 of 7

paste and paste0

paste glues pieces of text together, like linking train cars into one train. paste0 does it with no space in between, and you can choose your own separator.

In this page:

  1. paste and paste0
Syntax
r
paste(string1, string2, sep = " ")
paste0(string1, string2)
paste(vector_name, collapse = ", ")

paste and paste0

paste() concatenates strings together, inserting a separator (a space by default) between each argument. paste0() is shorthand for paste(..., sep = ""), concatenating with no separator at all.

Both also work element-wise on vectors, pairing up corresponding elements.

Note: paste0(a, b) is a common shortcut for building a string with no space, like a file path or identifier.

Example: paste and paste0

markup
first <- "Hello"
second <- "World"

cat(paste(first, second), "\n")
cat(paste0(first, second), "\n")
cat(paste(first, second, sep = "-"), "\n")
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
  1. Expecting paste("a", "b") to have no space, when the default separator is a space; use paste0() or sep = "".
  2. Passing a vector and expecting one string, when paste() gives a vector and you need collapse = ", ".
  3. Using + to join strings, which is an error in R.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.