0
0
R Programmingprogramming~10 mins

String formatting with sprintf in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to format the number 5 as a string using sprintf.

R Programming
result <- sprintf([1], 5)
Drag options to blanks, or click blank then click option'
A"%d"
B"%f"
C"%s"
D"%x"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%f" formats as floating point with decimals.
Using "%s" expects a string input, not a number.
Using "%x" formats as hexadecimal.
2fill in blank
medium

Complete the code to format the number 3.14159 to two decimal places using sprintf.

R Programming
result <- sprintf([1], 3.14159)
Drag options to blanks, or click blank then click option'
A"%.2f"
B"%.0f"
C"%.3f"
D"%d"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%d" truncates the number to integer.
Using "%.0f" rounds to zero decimals.
Using "%.3f" keeps three decimals, not two.
3fill in blank
hard

Fix the error in the code to format the string with a name and age using sprintf.

R Programming
result <- sprintf("Name: %s, Age: [1]", "Alice", 30)
Drag options to blanks, or click blank then click option'
A"%c"
B"%f"
C"%s"
D"%d"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%s" for age causes type mismatch.
Using "%f" formats as floating point.
Using "%c" expects a single character.
4fill in blank
hard

Fill both blanks to format a floating number with width 8 and 3 decimals using sprintf.

R Programming
result <- sprintf([1], [2])
Drag options to blanks, or click blank then click option'
A"%8.3f"
B3.14159
C8.12345
D"%6.2f"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%6.2f" formats with wrong width and decimals.
Passing a string instead of a number as second argument.
Mixing up width and precision in format.
5fill in blank
hard

Fill all three blanks to format a string with a name left-aligned in width 10 and an integer score padded with zeros to width 4.

R Programming
result <- sprintf([1], [2], [3])
Drag options to blanks, or click blank then click option'
A"%-10s %04d"
B"%04d"
C"Alice"
D42
Attempts:
3 left
💡 Hint
Common Mistakes
Using "%10s" right-aligns the string.
Using "%d" does not pad zeros.
Passing numbers as strings causes errors.