R print() Function
In R, we use the
print() function to print values and variables. For example,
# print valuesprint("R is fun")# print variables x <- "Welcome to Programiz"print(x)
 "R is fun"  "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
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
If you don't want any default separator between the string and variable, you can use another variant of
paste0(). For example,
company <- "Programiz"# using paste0() instead of paste() print(paste0("Welcome to", company))
 "Welcome toProgramiz"
Now, you can see there is no space between the string and the variable.
R sprintf() Function
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)
 "String: Welcome to Programiz"
String: %s- a formatted string
%s- format specifier that represents string values
myString- variable that replaces the format specifier
%s, there are many other format specifiers that can be used for different types values.
||Signed Decimal Integer|
||Decimal Floating Point|
||Unsigned Decimal Integer|
Let's use some of them in examples.
# sprintf() with integer variable myInteger <- 123sprintf("Integer Value: %d", myInteger)# sprintf() with float variable myFloat <- 12.34sprintf("Float Value: %f", myFloat)
 "Integer Value: 123"  "Float Value: 12.340000"
R cat() Function
R programming also provides the
cat() function to print variables. However, unlike
cat() function is only used with basic types like logical, integer, character, etc.
# print using Catcat("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.
Print Variables in R Terminal
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 x // Output:  "Welcome to Programiz"