Print a Formatted string in R Programming - sprintf() Function
Last Updated : 30 Apr, 2025
The sprintf()
function is used to create formatted strings in R. These formatted strings enable us to insert variables and values into a string while controlling their appearance and formatting. The sprintf()
function uses a user-defined format to return a formatted string by inserting the corresponding values from the provided list.
Syntax :
sprintf(format, values)
Parameter:
- format: Format of printing the values
- values: to be passed into format
In the given R program, sprintf()
is used to create a formatted string. The format string " % s to % s"
contains two %s
placeholders, which are meant for string values. The variables x1
and x2
are inserted into these placeholders, resulting in the formatted string "Welcome to GeeksforGeeks". sprintf()
replaces the placeholders with the values of x1
and x2
, allowing for dynamic and structured string formatting.
R x1 <- "Welcome" x2 <- "GeeksforGeeks" sprintf("% s to % s", x1, x2)
Output:
"Welcome to GeeksforGeeks"
In the given R program, sprintf()
is used to create a formatted string. The format string "%s gives %.0f percent %s"
contains three placeholders: %s
for strings and %.0f
for a formatted number (percentage). The variables x1
, x2
, and x3
are inserted into these placeholders. The value of x1
("GeeksforGeeks") replaces the first %s
, the value of x2
(100) is formatted as a whole number for %.0f
, and the value of x3
("success") replaces the last %s
.
R x1 <- "GeeksforGeeks" x2 <- 100 x3 <- "success" sprintf("% s gives %.0f percent % s", x1, x2, x3)
Output:
"GeeksforGeeks gives 100 percent success"
In the given R program, sprintf()
is used to create a formatted string. The format string "The mean is %.2f, and the standard deviation is %.2f"
contains two %.2f
placeholders, which are used to format the numeric values mean_value
and standard_deviation
to two decimal places. The mean_value
(35.68) and standard_deviation
(7.42) are inserted into these placeholders, resulting in the formatted string
R mean_value <- 35.68 standard_deviation <- 7.42 formatted_string <- sprintf("The mean is %.2f, and the standard deviation is %.2f", mean_value, standard_deviation) cat(formatted_string)
Output:
The mean is 35.68, and the standard deviation is 7.42
In this article, we explored how to create formatted strings in R using the sprintf()
function.
Similar Reads
Convert an Object to a String in R Programming - toString() Function toString() function in R Language is used to convert an object into a single character string. Syntax: toString(x, width) Parameters: x: Object width: maximum string width Example 1: Python3 1== # R program to convert an object to string # Creating a vector x <- c("Geeks", "for
1 min read
Formatting Numbers and Strings in R Programming - format() Function In R programming, the format() function formats numbers, strings and dates to meet presentation needs. It gives formatting features to modify the display of numeric values, strings and date/time information. This function is applied to regulate the number of decimal places, alignment, scientific not
3 min read
Create Repetitions of a String in R Programming - strrep() Function strrep() Function in R Language is used to create specified number of repetitions of a specified string. Syntax: strrep(string, n) Parameter: string: specified string n: number of repetitions Example 1: Python3 1== # R Program to illustrate # the use of strrep function # String to be repeated x <
1 min read
Convert a String into Date Format in R Programming - as.Date() Function as.Date() function in R Language is used to convert a string into date format. Syntax: as.Date(x, format) Parameters: x: string variable format: Format in which string is declared(%m/%d/%y) Example 1: Python3 1== # R program to convert string into date # Creating a string vector dates <- c("
1 min read
Print the Argument to the Screen in R Programming - print() Function print() function in R Language is used to print out the argument to the screen. Syntax: print(x, digits, na.print) Parameters: x: specified argument to be displayed digits: defines minimal number of significant digits na.print: indicates NA values output format Example 1: Python3 # R program to illu
2 min read