What Do \n, %d and {} Mean? Escape Sequences and Format Codes, Explained and Counted

The short answer: \n is an escape sequence for a new line, %d is a format specifier that prints a whole number, and {} is a placeholder that marks where a value goes. Which of them you type depends on the language and, above all, on its print function. We counted every code in 1,745 string and character literals: 52% of C strings contain one, against 2% in C++.
Key Findings
- •
386 codes in 1,745 literals across eight languages: 148 brace placeholders, 98 escapes, 76 % specifiers and 64
${}or$nameinterpolations. - •
C leads: 47 of its 91 strings contain a code, because
printfneeds%dto insert a value and\nto end the line. - •
Our Rust and Python code has no
\nat all.println!andprint()add the newline themselves. - •
{}is the most common code, 75 times, almost all in Rust.\nis second with 70. - •
C++ ends lines with
'\n'22 times and withstd::endlnever.
Source: TurboType analysis of 1,200 code snippets in C, C++, Java, JavaScript, TypeScript, PHP, Python and Rust, November 2026.
1. The Three Kinds of Code Inside Strings
A string is meant to hold text exactly as written, but programs constantly need things you cannot type literally: a line break, a tab, a quote inside quotes, or a value that is only known when the program runs. Languages solve that with short codes inside the string, and almost all of them belong to one of three families:
- •
Escape sequences start with a backslash and stand for one character:
\nis a new line,\ta tab,\"a quote. The compiler turns them into the real character before the program runs. - •
Format specifiers start with a percent sign and stand for a value supplied separately: in
printf("%d", age),%dis replaced by the number inage. They come from C. - •
Placeholders use braces:
{}in Rust and Python'sformat,{name}in an f-string,${name}in a JavaScript template literal. They are the modern replacement for % specifiers.
2. Escape Sequences: \n, \t and the Rest
The backslash tells the compiler that the next character is not meant literally. These are the escapes you will meet, and where they work among the eight languages we analysed:
| Escape | Means | Used for | Works in |
|---|---|---|---|
\n | Line feed, a new line | Ends a line on Linux and macOS | All eight |
\r | Carriage return | Windows text ends each line with \r\n | All eight |
\t | Tab | Moves to the next tab stop | All eight |
\\ | One backslash | Windows paths and regular expressions in normal strings | All eight |
\" | Double quote | A quote inside a double-quoted string | All eight |
\' | Single quote | A quote inside a single-quoted string or character | All eight |
\0 | Null character | Marks the end of every C string | C and C++; most others accept it |
\x41 | Character by hex code | \x41 is A | All except Java |
\u00e9, \u{e9} | Unicode character | Both are é; Rust and PHP use the braces form | All eight, in one form or the other |
\a \b \f \v | Bell, backspace, form feed, vertical tab | Leftovers from teleprinters | Mostly C, C++ and Python |
Some strings switch escapes off. Python raw strings, r"C:\new", keep every backslash, which is why regular expressions are written r"\d+". Rust has r"…", C++ has R"(…)" and JavaScript has String.raw. PHP single quotes are nearly raw: '\n' is a backslash and an n, and only \' and \\ are escapes.
An unknown escape is handled differently everywhere. Java refuses to compile "\d", C compilers warn, JavaScript quietly turns it into d, and Python 3.12 and later print a SyntaxWarning for an invalid escape sequence. That warning is the most common reason to switch a Python regular expression to a raw string. Our Java snippets show the alternative: 11 of their 12 escapes are \\, doubled backslashes inside regular expressions such as "\\s+".
3. Format Specifiers: %d, %s and %f
A format specifier is a slot in a template string. The function reads the template, and each specifier takes the next argument and prints it in the style the letter names. This is C's printf family, and it lives on in C++, Java's String.format and printf, PHP's printf and sprintf, Python's % operator and logging module, Go's fmt and the shell's printf.
| Specifier | Prints | Example | Output |
|---|---|---|---|
%d, %i | Integer, in decimal | printf("%d", 42) | 42 |
%s | String | printf("%s", "Ann") | Ann |
%f | Floating point, 6 decimals | printf("%f", 3.5) | 3.500000 |
%.2f | Floating point, 2 decimals | printf("%.2f", 3.14159) | 3.14 |
%e | Scientific notation | printf("%e", 1234.5) | 1.234500e+03 |
%c | One character | printf("%c", 'A') | A |
%x, %X | Hexadecimal | printf("%x", 255) | ff |
%o | Octal | printf("%o", 8) | 10 |
%u | Unsigned integer | printf("%u", 7u) | 7 |
%p | Pointer address (C, C++) | printf("%p", ptr) | an address such as 0x7ffd5e8c |
%5d | Width 5, right-aligned | printf("[%5d]", 42) | [ 42] |
%-8s | Width 8, left-aligned | printf("[%-8s]", "Ann") | [Ann ] |
%05d | Zero-padded to 5 digits | printf("%05d", 42) | 00042 |
%ld, %zu | long, size_t (C) | printf("%zu", sizeof(int)) | 4 on most systems |
%% | A literal percent sign | printf("100%%") | 100% |
Between the % and the letter you can add flags, a width and a precision: %08.3f means zero-padded, 8 characters wide, 3 decimals. In C the type must match exactly: %d with a double is undefined behaviour, and long needs %ld. Java checks at run time and throws an IllegalFormatConversionException instead.
%n is the one to watch. In Java it prints the platform's line separator, the portable way to end a line in printf. In C it prints nothing and writes the number of characters printed so far into a pointer argument, which is rarely what anyone wants and a known security risk.
4. Placeholders: {} and f-strings
Brace placeholders do the same job as % specifiers with less to remember: {} takes the next argument whatever its type, and formatting options go after a colon. Python, Rust, C++20 and C# share almost the same mini-language.
| Language | Example | Notes |
|---|---|---|
| Python f-string | f"{name} is {age}" | Any expression inside the braces; {age:.1f} adds a format; {x=} prints the name and the value |
Python str.format | "{} is {}".format(name, age) | {0} picks an argument by position, {name} by keyword |
| Rust | println!("{} is {}", name, age) | {:?} prints the debug form, {:.2} two decimals, {name} uses a variable directly |
| C++20 | std::format("{} is {}", name, age) | The same mini-language as Python; C++23 adds std::println |
| Java (SLF4J logging) | log.info("User {} logged in", id) | Logging only; String.format uses % specifiers |
| C# | $"{name} is {age}" | Interpolated string; "{0}" in string.Format |
To print a literal brace in any of these, double it: "{{}}" prints {}. In Rust, println!("{name}") has been able to use a variable directly since Rust 1.58; our Rust snippets do that 15 times, against 73 bare {} placeholders.
5. Interpolation: ${} and $name
Some languages let the string name the variable directly, with no separate argument list.
- •
JavaScript and TypeScript:
`Hello ${name}`inside backticks only. In single or double quotes,${name}is printed as written, a frequent bug. Our TypeScript snippets use 32 of them, JavaScript 15. - •
PHP:
"Hello $name"and"{$user->name}"inside double quotes only. Our 55 double-quoted PHP strings insert 17 variables; the other 342 PHP strings are single-quoted. - •
Shell, Kotlin, Dart and Groovy use
$nameand${expression}too; Ruby uses#{name}.
Our single vs double quotes guide covers which quote turns interpolation on in each language.
6. What Each Language Actually Uses
We extracted every string and character literal from 1,200 snippets in our practice tracks and counted the codes inside them. The share of strings that contain at least one code varies more than twentyfold:
| Language | Strings | With a code | Escapes | % specifiers | {} placeholders | ${} or $name |
|---|---|---|---|---|---|---|
| C | 91 | 47 (52%) | 50 | 46 | 0 | 0 |
| Rust | 252 | 93 (37%) | 0 | 0 | 109 | 0 |
| Python | 228 | 30 (13%) | 3 | 5 | 37 | 0 |
| Java | 117 | 14 (12%) | 12 | 14 | 2 | 0 |
| TypeScript | 230 | 25 (11%) | 2 | 0 | 0 | 32 |
| JavaScript | 223 | 12 (5%) | 1 | 0 | 0 | 15 |
| PHP | 397 | 19 (5%) | 6 | 11 | 0 | 17 |
| C++ | 114 | 2 (2%) | 24 | 0 | 0 | 0 |
The print function explains almost all of it. printf neither inserts values nor ends lines on its own, so C strings carry both: "Age: %d\n" is typical, and 38 of C's 50 escapes are \n. Rust's println! and Python's print() add the newline themselves, so neither language has a single \n in our sample; Rust's 109 codes are all brace placeholders and Python's 37 f-string fields carry the values.
C++ is the opposite of C. std::cout << name inserts values without any code at all, and lines end with '\n', a character literal rather than a string: 22 times, against no std::endl. That matches the C++ Core Guidelines, which advise against std::endl because it also flushes the output on every line.
7. The Most Common Codes
Two codes stand out: {} (75) and \n (70). Among % specifiers, %d (32) and %s (28) account for 60 of 76; everything else is rare. \0 appears 10 times, all in C, where it terminates strings built by hand.
Codes are a small share of what you type, but they are dense with symbols: "%d\n" is four characters and three of them are symbols. Our guide to programming symbols names every one of them.
8. Typing Them
On a US keyboard the backslash is one key, above Enter on most keyboards and under the right little finger. % is Shift+5, $ is Shift+4 and the braces are Shift with the bracket keys. "%d\n" takes five key presses: Shift, 5, d, backslash, n.
On other layouts the same codes cost more. The backslash is AltGr+ß on a German keyboard and AltGr+8 on French AZERTY, and the braces need AltGr on German, French and Swedish keyboards. Our keyboard layout comparison shows where each one is.
To build the habit, practise where the codes are densest: the C track for printf formats and \n, the Rust track for {} and {:?}, and the Python track for f-strings.
9. Method and Limits
We analysed the 1,200 snippets in TurboType's C, C++, Java, JavaScript, TypeScript, PHP, Python and Rust tracks, 150 per language, and extracted 1,745 string and character literals, skipping comments. The HTML & CSS track was left out: its only escapes are HTML entities, two in the whole track, and % in CSS is a unit, not a specifier. We counted backslash escapes the language turns into a character, so not in raw strings or PHP single quotes; printf-style specifiers, skipping date formats such as %Y-%m-%d and template syntax such as {% for %}; brace placeholders in Rust strings, Python f-strings and .format strings and Java logging calls; ${} in template literals; and variables in PHP double quotes. Regular-expression quantifiers such as \d{4} are not placeholders and were excluded. The 1,745 literals include 93 character literals in C, C++, Java and Rust; the shares in Figure 1 count strings only, while the escape counts include characters such as '\n'.
Limits: 386 codes is a small sample, and practice snippets favour short programs that print a lot. The counts also reflect our snippets' style choices, for example std::cout rather than printf in C++. The tables of escapes and specifiers describe the languages and hold everywhere.
10. Frequently Asked Questions
What does \n mean in programming?
It is the escape sequence for a new line. Inside a string, a backslash followed by n is stored as one character, the line feed, which starts a new line when the text is printed. It works in C, C++, Java, JavaScript, Python, Rust and in PHP double quotes. Windows text files end lines with \r\n. In our 1,745 string and character literals, \n appeared 70 times, 62 of them in C and C++.
What does %d mean in C?
In printf, %d is a placeholder for an integer printed in decimal: printf("Age: %d\n", age) prints Age: 30. The other common specifiers are %s for a string, %f for a floating-point number, %c for a character, %x for hexadecimal and %% for a literal percent sign. %d was the most common specifier in our C code, 24 of 46.
What is the difference between %d and %i?
In printf there is none: both print a signed integer in decimal. They differ in scanf, where %d reads only decimal digits and %i also accepts hexadecimal with a 0x prefix and octal with a leading 0.
What does {} mean in a print statement?
It marks where the next value goes. Rust's println!("{}", x), Python's "{}".format(x) and C++20's std::format all replace each {} with the next argument. In a Python f-string the braces hold the value itself, as in f"{name}". Rust's {:?} prints the debug form of a value instead.
How do you print a literal %, { or backslash?
Double it. In printf-style formats, %% prints one percent sign. In Python format strings and f-strings, Rust and C++ std::format, {{ and }} print one brace each. In almost every language, \\ inside a string is one backslash. In a JavaScript template literal, write \${ to stop ${ starting a placeholder.
Next Steps:
- Type a few
printflines on the C track until%d\ncomes out without a pause. - Switch to the Rust track and notice how much shorter
println!lines are without escapes. - Take the 60-second coding typing test and check which codes show up in your errors.
Related Resources:
Every symbol, named and counted.
Which quote turns escapes and interpolation on.
Operators explained and counted.
Where every bracket is, and what AltGr costs.
