1 Definition and purpose

A string literal is a fixed textual value written directly in source code. It allows a programmer to embed characters, words, or longer passages without constructing them from individual parts at runtime. Because string literals are part of the program text, they are interpreted by the language’s parser and converted into a string value according to the language’s rules.

1.1 Literal values in programming languages

In programming languages, a literal is a notation that stands for a value exactly as written. String literals belong to the broader family of numeric, boolean, and character literals. They are commonly used when a program needs a constant message, label, path, or other text that should be readable in the source itself.

1.2 Text representation in source code

String literals provide a way to represent human-readable text inside code while preserving control over special characters. Their syntax must account for quotation marks, line breaks, and encoding details, since source files are ultimately sequences of bytes or characters processed by tools and compilers. The literal form often makes text easier to maintain than assembling it programmatically.

1.3 Common uses in software development

String literals appear in user-interface labels, logging messages, configuration data, regular expressions, file names, SQL statements, and test cases. They are also used in documentation examples and in simple data declarations. In many programs, they form one of the most visible parts of the source code because textual output is frequently expressed this way.

2 Syntax and notation

The exact notation for a string literal depends on the programming language. Most languages use delimiters to mark the start and end of the text, then provide rules for representing special characters within it. Some also offer alternate literal forms for raw text, multiline content, or interpolation.

2.1 Delimiters

Delimiters signal where the literal begins and ends. They reduce ambiguity for the parser and make it possible to distinguish text from identifiers, operators, and punctuation in the surrounding code.

2.1.1 Single quotes

Some languages use single quotes for string literals, though in others single quotes denote character literals instead. When supported, single-quoted strings are usually treated the same as double-quoted strings aside from escape behavior or interpolation rules.

2.1.2 Double quotes

Double quotes are among the most common delimiters for string literals. They are widely used for ordinary textual content, especially in languages where double-quoted strings support escape sequences and other features.

2.1.3 Triple quotes and multiline delimiters

Triple quotes or similar extended delimiters allow a string to span multiple lines more naturally. They are useful for formatted text, documentation blocks, and examples where preserving line breaks is important. Such forms often reduce the need to escape quotation marks inside the content.

2.2 Escape sequences

Escape sequences let a programmer insert characters that would otherwise be difficult to type directly or would conflict with the delimiter syntax. They usually begin with a backslash and are interpreted specially by the language.

2.2.1 Newline and tab escapes

Common escapes represent control characters such as newline, tab, carriage return, and backspace. These are often used to format output or to describe text structures that depend on spacing and line breaks.

2.2.2 Quote escaping

When a quote character appears inside a quoted string, it may need to be escaped so the parser does not treat it as the end of the literal. This is especially important in text containing contractions, dialogue, or nested quoted fragments.

2.2.3 Unicode and hexadecimal escapes

Many languages support escapes that specify characters by code point or byte value, often in hexadecimal form. These are useful for symbols that are not easily typed, for exact textual matching, or for representing characters in a portable way.

2.3 Raw string forms

Raw string forms reduce or disable escape processing so that backslashes and similar characters are treated more literally. They are often chosen for regular expressions, file paths, and other patterns where repeated escaping would make the source harder to read. The precise rules vary widely, and some raw forms still preserve limited syntax constraints.

3 Language-specific variations

Different languages implement string literals in different ways. Some favor simplicity, while others offer several specialized literal forms to support international text, formatted output, or source-code readability.

3.1 C and C-derived languages

C and related languages have influenced many later syntax conventions for strings. Their string literals often use quotation marks and backslash escapes, with additional prefixes or modifiers in newer language versions.

3.1.1 Standard string syntax

In C-like languages, standard strings are commonly enclosed in double quotes and terminated by a null character in memory. Escape sequences are used for special characters, and adjacent literals may be combined by the compiler in some cases.

3.1.2 Wide and UTF-prefixed literals

Many C-derived languages provide prefixes for wide-character or UTF-encoded string literals. These forms help express text in different encodings or character-width models and support internationalized applications more directly.

3.2 Python

Python supports several string literal forms with distinct prefix options. Its syntax is designed to accommodate ordinary text, raw patterns, and formatted interpolation in a compact and readable way.

3.2.1 Ordinary string literals

Ordinary Python strings can be written with single or double quotes, and triple-quoted forms can span multiple lines. Escape sequences are interpreted inside these literals, making them suitable for general text and formatted output.

3.2.2 Raw strings

Raw string literals in Python treat backslashes as ordinary characters in most cases. They are widely used for regular expressions and Windows-style file paths, where escape-heavy syntax would otherwise be cumbersome.

3.2.3 f-strings and interpolation

Formatted string literals, often called f-strings, allow expressions to be embedded directly inside a string form. This supports concise formatting while keeping the resulting text readable in the source.

3.3 JavaScript

JavaScript provides multiple string literal styles that serve different needs. Traditional quote-delimited strings coexist with template literals, which are especially useful for multiline text and embedded expressions.

3.3.1 String literals with quotes

JavaScript strings are commonly written with single or double quotes. Both forms are similar in meaning, and escape sequences are used to include special characters or quote marks inside the string.

3.3.2 Template literals

Template literals use backticks and can span multiple lines. They support expression interpolation and are often preferred when building text that combines fixed content with dynamic values.

3.4 Java and C#

Java and C# have similar general approaches to string syntax, though each language adds its own forms for handling Unicode and multiline text. Their literals are closely tied to the languages’ character and string type systems.

3.4.1 Unicode handling

These languages define string values in terms of Unicode-compatible internal models, though implementation details differ. Literal syntax may include escapes for Unicode code points and other special characters, allowing programs to express text precisely.

3.4.2 Verbatim and text block forms

Java and C# offer literal forms that preserve text more directly than standard quoted strings. Verbatim or text block syntax is useful for multiline content, source snippets, and paths that contain many backslashes or quotation marks.

4 Internal representation

A string literal in source code is transformed into an internal string value by the compiler, interpreter, or runtime system. The resulting representation may depend on encoding rules, character model, and implementation choices.

4.1 Character encodings

Encodings determine how textual characters are mapped to stored values. The same literal can represent different byte sequences depending on the source-file encoding and the language’s internal character representation.

4.1.1 ASCII

ASCII is a compact character set that covers basic Latin letters, digits, punctuation, and control codes. Older systems and simple programs may rely on ASCII-compatible assumptions, though this limits direct representation of many world scripts and symbols.

4.1.2 Unicode

Unicode provides a far broader character repertoire and is the dominant basis for modern text processing. Many languages now treat string literals as Unicode text, either directly or through encodings such as UTF-8, UTF-16, or UTF-32.

4.2 Memory storage and immutability

String values are often stored in memory as arrays or sequences of code units. In many languages, strings are immutable, meaning the literal’s characters cannot be altered in place after creation. This property can simplify sharing, caching, and safe reuse by the runtime.

4.3 Compile-time handling

Compilers and interpreters may process string literals before execution begins. They can resolve escapes, merge adjacent literals, intern repeated values, or place constant text into read-only storage. Such handling can improve efficiency and preserve consistency across the program.

5 Operations on string literals

Although a string literal begins as source text, it often participates in further operations such as joining, formatting, or comparing. These operations may occur at compile time or during program execution.

5.1 Concatenation

Concatenation combines two or more strings into one longer string. Some languages allow this to happen automatically under certain conditions, while others require an explicit operator or function call.

5.1.1 Compile-time concatenation

In some languages, adjacent literals are merged by the compiler into a single value. This can improve readability by splitting a long text across multiple lines without changing the resulting string.

5.1.2 Runtime concatenation

Runtime concatenation occurs when values are combined while the program runs. It is often used when text depends on variables, user input, or other dynamic data.

5.2 Interpolation and formatting

Interpolation inserts computed values into a string template. Formatting systems may also control alignment, precision, or number and date presentation. These features are common in modern languages because they reduce manual string assembly.

5.3 Comparison and parsing

String literals are frequently compared with other strings to make decisions or validate input. They are also used in parsers and interpreters as fixed tokens, reserved words, or pattern fragments. Because exact spelling matters, comparisons are sensitive to case, encoding, and normalization rules.

6 Special forms and edge cases

String syntax often includes special situations that require additional rules. These edge cases can be important for correctness, readability, and portability.

6.1 Empty string literals

An empty string literal represents a string with no characters. It is commonly written as two adjacent delimiters with nothing between them and is often used as a default value or neutral starting point.

6.2 Multiline strings

Multiline strings preserve line breaks across several lines of source code. They are useful for long text passages, embedded markup, generated output, and data samples. Some languages retain leading indentation while others provide mechanisms to trim it.

6.3 Embedded null characters

A string literal may contain a null character in languages that support it, but this can be significant in systems influenced by C conventions. In such environments, embedded nulls may affect APIs that treat null as a terminator rather than as ordinary data.

6.4 Adjacent literal concatenation

Some languages allow two string literals written next to each other to be treated as one. This feature can improve formatting of lengthy messages, though it may also create subtle errors if whitespace or punctuation is omitted unintentionally.

7 Security and correctness concerns

String literals can contribute to bugs or vulnerabilities when they are used unsafely or encoded incorrectly. Careful handling is important when literals interact with user-supplied data, external systems, or multiple character sets.

7.1 Injection risks

If a string literal is used to build commands, queries, or markup by simple concatenation with external input, it may enable injection problems. Safer approaches usually separate data from code and rely on parameterization or escaping routines.

7.2 Improper escaping

Incorrect escaping can change a literal’s meaning or break parsing altogether. A missing backslash, mismatched delimiter, or mistaken control sequence may produce syntax errors or unexpected runtime text.

7.3 Encoding mismatches

If the source file encoding, compiler settings, and runtime expectations do not agree, the characters represented by a literal may be corrupted or misread. Such mismatches can affect display, comparison, file handling, and interoperability.

String literals are closely connected to other language constructs that deal with textual data. These related forms differ in how they are written, stored, and manipulated.

8.1 Character literals

Character literals represent a single character rather than a sequence. They often use similar delimiters or escape rules, but their meaning and type are distinct from those of string literals.

8.2 String objects and string types

String objects are runtime values that store text, while string types define how a language models and manipulates that text. A string literal usually produces an instance of such a type when the program is compiled or executed.

8.3 String templates and format strings

String templates and format strings are mechanisms for combining fixed text with variable content. They are related to literals because they often begin as literal text fragments that are then expanded or formatted according to runtime values.