R Print Output

[This article was first published on R feed, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R print() Function

In R, we use the print() function to print any values and variables. For example,

# print values
print("R is fun")

# print variables
x <- "Welcome to Programiz"


[1] "R is fun"
[1] "Welcome to Programiz"

In the above example, we have used the print() function to print a string and a variable. When we use a variable inside print(), it prints the value stored inside the variable.

paste() Function in R

You can also print a string and variable together using the print() function. For this, you have to use the paste() function inside print(). For example,

company <- "Programiz"

# print string and variable together
print(paste("Welcome to", company))


Welcome to Programiz

Notice the use of the paste() function inside print(). The paste() function takes two arguments:

  • string - "Welcome to"
  • variable - company

By default, you can see there is a space between string Welcome to and the value Programiz.

If you don't want any default separator between the string and variable, you can use another variant of paste() called paste0(). For example,

company <- "Programiz"

# using paste0() instead of paste()
print(paste0("Welcome to", company))


[1] "Welcome toProgramiz"

Now, you can see there is no space between the string and the variable.

R sprintf() Function

The sprintf() function of C Programming can also be used in R. It is used to print formatted strings. For example,

myString <- "Welcome to Programiz"

# print formatted string
sprintf("String: %s", myString)


[1] "String: Welcome to Programiz"


  • String: %s - a formatted string
  • %s - format specifier that represents string values
  • myString - variable that replaces the format specifier %s

Besides %s, there are many other format specifiers that can be used for different types values.

Specifier Value Type
%c Character
%d or %i Signed Decimal Integer
%e or %E Scientific notation
%f Decimal Floating Point
%u Unsigned Decimal Integer
%p Pointer address

Let's use some of them in examples.

# sprintf() with integer variable
myInteger <- 123
sprintf("Integer Value: %d", myInteger)

# sprintf() with float variable
myFloat <- 12.34
sprintf("Float Value: %f", myFloat)


[1] "Integer Value: 123"
[1] "Float Value: 12.340000"

R cat() Function

R programming also provides the cat() function to print variables. However, unlike print(), the cat() function is only used with basic types like logical, integer, character, etc.

# print using Cat
cat("R Tutorials\n")

# print a variable using Cat
message <- "Programiz"
cat("Welcome to ", message)


R Tutorials
Welcome to  Programiz

In the example above, we have used the cat() function to display a string along with a variable. The \n is used as a newline character.

Note: As mentioned earlier, you cannot use cat() with list or any other object.

You can also print variables inside the R terminal by simply typing the variable name. For example,

# inside R terminal
x = "Welcome to Programiz!"

# print value of x in console

// Output: [1] "Welcome to Programiz"

To leave a comment for the author, please follow the link and comment on their blog: R feed.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)