I'm trying to understand why nested string or argument escaping usually inherits the current escape depth, forcing inner quotes and backslashes to be escaped again. For example, a deeply nested value can become difficult to read because every layer adds more backslashes and escaped quotes.
Why not use distinct, numbered delimiters for each nesting level instead? Conceptually, the outer layer could use delimiters such as 【3】 and 【/3】, the next layer 【2】 and 【/2】, and so on. Adding another layer would only require changing the outermost delimiter number, without modifying the contents inside it. This could still support multiple independent nested sections, provided their delimiter levels remain properly ordered.
The obvious tradeoff is that the chosen delimiter syntax could no longer appear literally without its own escape mechanism. Is the conventional approach mainly historical and convenient, or are there deeper reasons involving parsing, arbitrary string representation, language design, or the fact that the outer parser usually cannot understand the inner language?
4 Answers
The outer parser generally has no knowledge of what the inner text means. It sees a string, while the inner language is parsed later by a different tool. Giving the outer parser numbered nesting levels would make it responsible for understanding and preserving structure that may belong to several unrelated languages. Keeping each layer independent is less elegant visually, but much simpler for language implementations and interfaces.
There are formats that use alternative delimiters, configurable delimiters, heredocs, raw strings, or length-prefixed strings. Those approaches are useful when large embedded blocks are common, but they add syntax and rules. For ordinary strings, repeated escaping is usually considered adequate because needing many nested levels is uncommon and often indicates that the data should be passed through a structured interface instead of assembled into one giant literal.
Regexes and heredocs are good examples of deliberately changing the delimiter when embedding one language inside another, but they still need a way to express the delimiter itself.
A string literal’s main job is to represent arbitrary text, not to provide a general-purpose nesting system. If the delimiter characters can appear in the data, the language needs a way to represent those characters too. Numbered delimiters don’t eliminate escaping; they move the problem to the delimiter syntax itself. A syntax should ideally be able to describe its own delimiters unambiguously, and that usually requires another escape rule or a length-based mechanism.
That’s the important catch: choosing uncommon brackets only reduces the chance of collisions; it cannot guarantee that arbitrary input won’t contain them.
Numbered wrappers would also need precise rules for malformed or partially unwrapped input, reused levels, adjacent nested regions, and text that contains wrapper-looking sequences. Traditional escaping has a useful property: every special character can be reduced to ordinary data by escaping it, and the parser can process the string one layer at a time. It may be harder to read, but the behavior is local and predictable.
So the repeated escapes are less about preserving an elegant visual representation and more about guaranteeing that every possible character sequence has an unambiguous interpretation.

This is also why libraries normally handle escaping at the boundary where data enters a database, shell, regular expression, or markup language. Manually stacking several languages is where the backslashes become especially painful.