String New Line: How to Write a Line Break in a String Literal
How to put a new line in a string literal: the \n escape, CRLF vs LF, multiline and raw strings, examples for common languages, and how to verify the result.
To put a line break inside a string, most programming languages use the escape sequence \n. You type two characters in your source code, a backslash and the letter n, and the program stores one invisible character: the line feed (LF, Unicode U+000A). When the string is printed or saved, the text continues on a new line at that point. This guide explains what that transformation does, how the syntax differs between languages, where it silently fails, and how to confirm that your string contains the break you intended.
What a newline escape does to your text
A string literal is text written directly in source code between quotes. Some characters can't be typed there safely, or at all, so languages define escape sequences: short backslash codes that the compiler or interpreter replaces with the real character. For a newline, the input is the two-character sequence \n inside a normal (non-raw) string. The output is a single line feed character in the resulting string value. Nothing else in the string changes.
The reverse operation also matters. When you take text that already contains real line breaks and need to paste it into a single-line literal, such as a JSON value or a JavaScript string, each line break must be converted back into \n. That conversion is called escaping. Turning \n back into a real break is called unescaping.
Line feed, carriage return and CRLF
There is more than one newline convention, and mixing them causes subtle bugs:
| Escape | Name | Code point | Typical use |
| \n | Line feed (LF) | U+000A | Linux, macOS, most programming contexts, JSON, web APIs |
| \r | Carriage return (CR) | U+000D | Rarely alone today; part of CRLF |
| \r\n | CR followed by LF (CRLF) | U+000D U+000A | Windows text files, many network protocols such as HTTP headers |
In most code, \n is the right choice. Use \r\n only when a file format or protocol explicitly requires CRLF. If you need whatever the current operating system uses, most languages offer a constant: os.linesep in Python, System.lineSeparator() in Java, Environment.NewLine in C# and os.EOL in Node.js.
How to write a newline in common languages
The table below shows the usual way to write a line break in an ordinary string and the main multiline or raw alternative. Raw strings matter because they do not interpret escapes, so \n inside them stays as a literal backslash and n.
| Language | Newline in an ordinary string | Multiline or raw alternative |
| Python | "a\nb" | Triple quotes """...""" allow real line breaks; r"a\nb" keeps backslash-n as two characters |
| JavaScript / TypeScript | "a\nb" or 'a\nb' | Backtick template literals ... allow real line breaks |
| Java | "a\nb" | Text blocks """ ... """ allow real line breaks |
| C# | "a\nb" | Verbatim @"..." does not process \n; raw string literals """ ... """ allow real line breaks |
| C / C++ | "a\nb" | C++ raw strings R"(...)" do not process escapes |
| Go | "a\nb" | Backtick raw strings allow real line breaks and do not process escapes |
| Rust | "a\nb" | Ordinary strings may span lines; raw strings r"..." do not process escapes |
| PHP | "a\nb" (double quotes only) | Single quotes keep \n literal; heredoc allows real line breaks |
| Ruby | "a\nb" (double quotes only) | Single quotes keep \n literal; heredoc allows real line breaks |
| JSON | "a\nb" | No raw or multiline form; a real line break inside a JSON string is invalid |
| Bash | $'a\nb' or printf 'a\nb' | Plain "a\nb" keeps backslash-n as two characters |
| PowerShell | "anb" (backtick, double quotes only) | Single quotes keep n literal; here-strings @" ... "@ allow real line breaks |
| SQL | Depends on the database | Concatenate CHAR(10) in SQL Server or MySQL, CHR(10) in PostgreSQL or Oracle; PostgreSQL also accepts E'a\nb' |
Example: Python
text = "First line\nSecond line"
print(text)
print(repr(text))
print(len("a\nb")) # expected: 3 (a, line feed, b)
print(len(r"a\nb")) # expected: 4 (raw string keeps the backslash)
Example: JavaScript
const escaped = "First line\nSecond line";
const template = `First line
Second line`;
console.log(escaped === template); // expected: true
console.log(JSON.stringify(escaped)); // shows the break as \n
Example: Bash
printf 'First line\nSecond line\n'
text=$'First line\nSecond line'
echo "$text"
In shell scripts, prefer printf over echo -e. Whether echo interprets \n varies between shells and systems, while printf handles escapes consistently in its format string.
Step by step: adding a newline and checking the result
- Identify the target: a source-code literal in a specific language, a JSON value, a shell command or a database query. The rules differ for each.
- Check the quote type. In PHP, Ruby, Bash, PowerShell and similar languages, single quotes usually disable escapes, so \n will not become a line break there.
- Make sure the literal is not raw. Prefixes such as r in Python, @ in C# or R in C++, and backticks in Go, keep \n as two literal characters.
- Insert \n where the break should appear, or \r\n if the format requires CRLF.
- Verify the stored value rather than trusting how it looks on screen: print its debug representation (repr in Python, JSON.stringify in JavaScript) or check its length. A correct single newline adds exactly one character.
- If the string is written to a file, check the bytes or the line-ending indicator in your editor. Python, for example, translates \n to the platform line ending when writing in text mode unless you pass newline="" or newline="\n".
Escaping multiline text for a string literal
String literal escaping tools, and functions such as json.dumps in Python or JSON.stringify in JavaScript, take plain text as input and produce text that can be placed safely between quotes. For newlines, every real line break becomes \n (a Windows CRLF break becomes \r\n). Characters that would otherwise end or corrupt the literal are escaped too, most often double quotes and backslashes. The meaningful content stays the same; only its representation changes.
Example input, two lines of plain text:
Hello "world"
Path: C:\temp
Example output, escaped for a double-quoted JSON or JavaScript string:
"Hello \"world\"\nPath: C:\\temp"
Unescaping reverses the process: \n becomes a real line break, " becomes a quote and \ becomes a single backslash. Specific tools differ in which characters they escape and which target languages they support, and this guide does not evaluate any particular tool, so check the tool's documented rules against the language you are targeting.
Common mistakes
- Double escaping: running text through an escaper twice turns \n into \n, which displays as a visible backslash-n instead of a line break.
- Using \n in a raw or single-quoted string and expecting a break.
- Pasting text copied on Windows and unexpectedly getting \r\n, or stray carriage returns that show up as ^M or break string comparisons.
- Putting a real line break inside a JSON string instead of \n, which makes the JSON invalid.
- Forgetting a trailing newline when a file format or command-line tool expects each line, including the last, to end with one.
- Comparing strings that look identical but use different line endings; normalize to \n before comparing if the source is uncertain.