Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Print a Formatted string in R Programming - sprintf() Function
Next article icon

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 format

Example 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.42

In this article, we explored how to create formatted strings in R using the sprintf() function.


Next Article
Print a Formatted string in R Programming - sprintf() Function

N

nidhi_biet
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences