Tips & Tricks•9 min read•

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

Yash Rai
Yash Rai
Founder, TurboType
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 $name interpolations.

  • •

    C leads: 47 of its 91 strings contain a code, because printf needs %d to insert a value and \n to end the line.

  • •

    Our Rust and Python code has no \n at all. println! and print() add the newline themselves.

  • •

    {} is the most common code, 75 times, almost all in Rust. \n is second with 70.

  • •

    C++ ends lines with '\n' 22 times and with std::endl never.

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: \n is a new line, \t a 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), %d is replaced by the number in age. They come from C.

  • •

    Placeholders use braces: {} in Rust and Python's format, {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:

EscapeMeansUsed forWorks in
\nLine feed, a new lineEnds a line on Linux and macOSAll eight
\rCarriage returnWindows text ends each line with \r\nAll eight
\tTabMoves to the next tab stopAll eight
\\One backslashWindows paths and regular expressions in normal stringsAll eight
\"Double quoteA quote inside a double-quoted stringAll eight
\'Single quoteA quote inside a single-quoted string or characterAll eight
\0Null characterMarks the end of every C stringC and C++; most others accept it
\x41Character by hex code\x41 is AAll except Java
\u00e9, \u{e9}Unicode characterBoth are é; Rust and PHP use the braces formAll eight, in one form or the other
\a \b \f \vBell, backspace, form feed, vertical tabLeftovers from teleprintersMostly 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.

SpecifierPrintsExampleOutput
%d, %iInteger, in decimalprintf("%d", 42)42
%sStringprintf("%s", "Ann")Ann
%fFloating point, 6 decimalsprintf("%f", 3.5)3.500000
%.2fFloating point, 2 decimalsprintf("%.2f", 3.14159)3.14
%eScientific notationprintf("%e", 1234.5)1.234500e+03
%cOne characterprintf("%c", 'A')A
%x, %XHexadecimalprintf("%x", 255)ff
%oOctalprintf("%o", 8)10
%uUnsigned integerprintf("%u", 7u)7
%pPointer address (C, C++)printf("%p", ptr)an address such as 0x7ffd5e8c
%5dWidth 5, right-alignedprintf("[%5d]", 42)[   42]
%-8sWidth 8, left-alignedprintf("[%-8s]", "Ann")[Ann     ]
%05dZero-padded to 5 digitsprintf("%05d", 42)00042
%ld, %zulong, size_t (C)printf("%zu", sizeof(int))4 on most systems
%%A literal percent signprintf("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.

LanguageExampleNotes
Python f-stringf"{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
Rustprintln!("{} is {}", name, age){:?} prints the debug form, {:.2} two decimals, {name} uses a variable directly
C++20std::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 $name and ${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:

Bars of the share of string literals that contain an escape, a format specifier or a placeholder: C 52 percent, Rust 37, Python 13, Java 12, TypeScript 11, JavaScript 5, PHP 5, C++ 2.
Figure 1. Share of string literals that contain an escape, a % specifier or a placeholder.
LanguageStringsWith a codeEscapes% specifiers{} placeholders${} or $name
C9147 (52%)504600
Rust25293 (37%)001090
Python22830 (13%)35370
Java11714 (12%)121420
TypeScript23025 (11%)20032
JavaScript22312 (5%)10015
PHP39719 (5%)611017
C++1142 (2%)24000

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

Bars of the 12 most common codes inside strings. Empty braces 75, backslash n 70, dollar-brace 47, f-string braces 37, percent d 32, percent s 28, braces with a format spec 21, PHP dollar variables 17, named braces 15, double backslash 12, backslash zero 10, percent f 9.
Figure 2. The 12 most common codes inside strings, across all eight languages.

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:

  1. Type a few printf lines on the C track until %d\n comes out without a pause.
  2. Switch to the Rust track and notice how much shorter println! lines are without escapes.
  3. Take the 60-second coding typing test and check which codes show up in your errors.