← Back to R Course | Chapter 6: Strings | Lesson 7 of 7

sprintf का उपयोग

sprintf एक text template में blanks को आपकी values से भरता है, एक form letter की तरह जहाँ हर blank सही नाम से stamp होता है। यह decimals जैसी चीज़ें भी control करता है।

In this page:

  1. sprintf
Syntax
r
sprintf("format %s %d %.2f", string_value, integer_value, decimal_value)

sprintf

sprintf() C-style format specifiers इस्तेमाल करते हुए एक string में values format करता है, जैसे strings के लिए %s, integers के लिए %d, और fixed decimal places के लिए %.2f।

यह formatting पर precise control देता है जो paste() offer नहीं करता, जैसे zero-padding या fixed-width fields। यह कई दूसरी languages में मिलने वाली printf-style formatting का R equivalent है।

Note: sprintf("%.2f", x) display के लिए एक number को exactly 2 decimal places तक format करने का standard तरीका है।

उदाहरण: sprintf

markup
name <- "Alice"
score <- 92.5

cat(sprintf("Name: %s, Score: %.1f%%\n", name, score))
cat(sprintf("Padded number: %05d\n", 42))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. 92.5 जैसे एक decimal number के साथ %d लिखना, जो एक error देता है; %f या %.1f इस्तेमाल करें।
  2. literal percent sign के लिए %% भूल जाना, इसलिए format टूट जाता है।
  3. format specifiers से कम values pass करना, जो too few arguments देता है।
चैप्टर सारांश
  • paste(), paste0(), nchar(), substr(), और case functions strings बनाते और inspect करते हैं।
  • gsub(), sub(), और strsplit() text replace और split करते हैं।
  • sprintf() values को strings में format करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.