Print a Formatted string in R Programming - sprintf() Function Last Updated : 30 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 formatExample 1: 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"Example 2: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"Example 3: 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.42In this article, we explored how to create formatted strings in R using the sprintf() function. Comment More infoAdvertise with us Next Article Print a Formatted string in R Programming - sprintf() Function N nidhi_biet Follow Improve Article Tags : R Language R-basics R String-Functions 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 Printing out to the Screen or to a File in R Programming - cat() Function cat() function in R Language is used to print out to the screen or to a file. Syntax: cat(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)Parameters: ...: atomic vectors, names, NULL and objects with no output file: the file in which printing will be done sep: specified separ 2 min read String Concatenation in R Programming String concatenation is a way of appending two or more strings into a single string whether it is character by character or using some special character end to end. There are many ways to perform string concatenation. Example: Input: str1 = 'Geeks' str2 = 'for' str3 = 'Geeks' Output: 'GeeksforGeeks 3 min read Convert a String to an Expression in R Programming - parse() Function parse() function in R Language is used to convert an object of character class to an object of expression class. Syntax: parse(text = character) Parameters: character: Object of character class Example 1: Python3 1== # R program to convert # character to expression # Creating an object of character 1 min read Convert an Integer to UTF8 in R Programming - intToUtf8() Function intToUtf8() function in R Language is used to convert an integer into UTF8 value. Syntax: intToUtf8(x, multiple) Parameters: x: Integer or integer vector multiple: Boolean value to convert into single or multiple strings Example 1: Python3 1== # R program to convert an integer to UTF8 # Calling the 1 min read Convert String to Single Quote Text in R Programming - sQuote() Function sQuote() function in R Language is used to convert the given string or character vector into single quote text. Syntax: sQuote(x) Parameters: x: specified string, character vector Example 1: Python3 # R program to illustrate # sQuote function # Initializing a string x <- "GeeksforGeeks" 1 min read Like