# Escaping newlines in multi-line string macros?

**URL:** https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240
**Category:** General Usage
**Tags:** macros, strings
**Created:** [May 22, 2025, 7:39am UTC](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240 "2025-05-22T07:39:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)
#### Post date: [May 22, 2025, 7:39am UTC](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240/1 "2025-05-22T07:39:10Z")

</div>

Would this feature, to be able to write a single string literal on multiple lines of the source code without `\n` in the string,

> [@Single string on multiple lines without \`\\n\`](https://discourse.julialang.org/t/single-string-on-multiple-lines-without-n/18099/7):
>
> Old topic, but worth updating I think. As per Julia 1.7 release notes: " A backslash (\ ) before a newline inside a string literal now removes the newline while also respecting indentation" julia\> "a \ b \ c" "a b c"

be eventually implemented for all string-like objects?

I was surprised that it doesn’t work for `StrFormat`:

```julia
using StrFormat

# This string is equivalent to "abc".
s = "a\
 b\
    c"

# This string is an error.
t = f"a\
 b\
    c"

```

Perhaps one needs to submit a feature request to each package that offers string-like literals?

(By the way, how this work? Does this mean that `StrFormat` does not use the parser of `String`?)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 22, 2025, 8:28pm UTC](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240/2 "2025-05-22T20:28:29Z")

</div>

> [@ryofurue](#):
>
> (By the way, how this work? Does this mean that `StrFormat` does not use the parser of `String`?)

`StrFormat`’s literal `f"..."` syntax is an example of a [“string macro” or non-standard string literal](https://docs.julialang.org/en/v1/manual/metaprogramming/#meta-non-standard-string-literals).

The original [PR julia#40753 about escaping newlines](https://github.com/JuliaLang/julia/pull/40753) explicitly says that is only for “non-custom strings”.

The basic reason for this, I think, is that custom strings `foo"..."` are parsed very differently than standard strings `"..."` in that Julia does (almost) _no escaping_ for custom strings — they are passed literally as-is to the macro (which can then implement whatever escaping it wants). Backslashes are not treated as escapes (except before the trailing `"`, see below).

This is very important for e.g. regex parsing, in that if you do `r"\s+"` you want the `\s` to be passed literally to the regex engine. If you used a standard string, e.g. with the `Regex` constructor, you would have to escape the backslash: `Regex("\\s+")`. This quickly becomes unreadable for complicated regexes. (Another example is that people often express Windows paths as `raw"..."` strings, which are simply an identity-function string macro, in order to avoid escaping all the backslashes.)

So, I guess the thought is that treating a `\` followed by a newline in a string macro as an escape would be odd in that context, since `\` inside a string macro is otherwise mostly treated as a literal backslash (except before the trailing `"`, which uses a [mind-numbing escaping rule](https://github.com/JuliaLang/julia/pull/24621)). Though it wouldn’t be impossible AFAICT.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 22, 2025, 8:35pm UTC](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240/3 "2025-05-22T20:35:39Z")

</div>

Note that you can always use triple-quoted strings `""" ... """` in order to have a multi-line string literal. This works with string macros too.

(So I don’t quite see what purpose was served by implementing backslash-escaped newlines for single-quoted strings in the first place. It isn’t explained in the [PR](https://github.com/JuliaLang/julia/pull/40753).)

---

<div class="post-metadata">

### Author: ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)
#### Post date: [May 23, 2025, 6:37am UTC](https://discourse.julialang.org/t/escaping-newlines-in-multi-line-string-macros/129240/4 "2025-05-23T06:37:41Z")

</div>

> [@stevengj](#):
>
> Note that you can always use triple-quoted strings `""" ... """` in order to have a multi-line string literal. This works with string macros too.
> 
> (So I don’t quite see what purpose was served by implementing backslash-escaped newlines for single-quoted strings in the first place. It isn’t explained in the [PR](https://github.com/JuliaLang/julia/pull/40753).)

I apologize beforehand if I’m mistaken but it seems to me that you mistake the issue. You mention “backslash-escaped newlines” but that’s not the subject of the present discussion. With the triple quoted string, you get newlines embedded in the string:

```julia
s = """a
 b
    c"""
# -> "a\nb\n c"

```

whereas the feature we are talking about is _to prevent newlines from entering the `String` object._

The original issue was/is that without this feature, you would write something like this:

```julia
s = "$(somevar) blah blah" *
   "blah blah $(othervar) blah blah" *
   "blah blah $(something) blah blah"

```

to get a string object that doesn’t include newline characters.
