Concepts / String Literals and Delimiters

String Literals and Delimiters

An example of a literal constant is a number like 5 , 1.23 , or a string like 'This is a string' or "It's a string!" .

  • Programming

What Is a String Literal?

A string literal is a sequence of characters written directly into your code that represents a fixed text value. Unlike a variable, which holds a value that can change, a string literal is a constant—it is always exactly what you write. When you write 'This is a string' or "It's a string!" directly in your code, you are creating a string literal. The characters between the opening and closing delimiters form the literal value.

A string literal is not a variable name; it is the actual text value itself. The moment you write it in your code, that exact text exists as a constant in your program.

How Delimiters Mark String Boundaries

Delimiters are the punctuation marks that tell the interpreter where a string begins and where it ends. The most common delimiters are single quotes (') and double quotes ("). When the interpreter encounters an opening delimiter, it knows to start collecting characters as part of a string. When it encounters the matching closing delimiter, it knows the string has ended. Everything between the pair belongs to the string literal.

string startsstring ends'opening delimiterTstring contenthstring contentistring contentsstring content'closing delimiter
How do opening and closing delimiters mark the start and end of a string, and what happens when they don't match?

The opening and closing delimiters must match. If you open with a single quote, you must close with a single quote. If you open with a double quote, you must close with a double quote. Mismatched delimiters cause a syntax error.

Single Quotes vs. Double Quotes

In most programming languages, single quotes and double quotes are functionally equivalent when used as delimiters for string literals. Both create a string with the same content. The choice between them is often a matter of style or convenience, but it becomes important when your string contains one type of quote character and you need to use the other type as the delimiter.

Delimiter TypeExampleWhen to UseAdvantage
Single quotes'This is a string'When your string contains double quotesAvoids the need to escape the quote inside
Double quotes"This is a string"When your string contains single quotesAvoids the need to escape the quote inside
Either'Hello' or "Hello"When your string contains neither type of quoteChoose whichever matches your code style

Suppose you want to create a string that says: It's a beautiful day. If you use single quotes as your delimiters, the apostrophe in 'It's' will be interpreted as a closing delimiter, breaking your string. Instead, you would use double quotes: "It's a beautiful day". Alternatively, you could use single quotes and escape the apostrophe, but using the opposite delimiter is cleaner.

What Happens Inside the Delimiters

Once the interpreter recognizes an opening delimiter, it treats every character that follows as part of the string content—including spaces, punctuation, numbers, and special characters—until it encounters the matching closing delimiter. Whitespace, newlines, and symbols are all preserved exactly as written. This means a string literal can span multiple lines in some languages, and spaces at the beginning or end are part of the string, not ignored.

Everything between the delimiters is literal. If you write 'hello world' with extra spaces, those spaces are part of the string. If you write 'line1\nline2', the backslash and n are two separate characters unless your language interprets escape sequences.

Escaping Delimiters Within Strings

Sometimes you need to include the same character that serves as your delimiter inside the string itself. To do this, you use an escape character—typically a backslash (\)—placed immediately before the delimiter. The escape character tells the interpreter to treat the following character as a literal character, not as a delimiter. For example, if you want to include a double quote inside a double-quoted string, you write \" to escape it.

"opening delimiterShe saidstring content\escape character"literal quote (not adelimiter)hellostring content\escape character"literal quote (not adelimiter)"closing delimiter
How do I include a quote character inside a string that's already delimited by quotes?

If you write "She said \"hello\" to me", the backslashes tell the interpreter that the two double quotes inside are literal characters, not delimiters. The string value is: She said "hello" to me. Without the escape characters, the interpreter would see the first " after 'said' as the closing delimiter, and the rest would be a syntax error.

String Literals vs. String Variables

A string literal is the actual text written in your code between delimiters. A string variable is a name that holds a reference to a string value. When you write s = 'Swaroop', you are assigning the string literal 'Swaroop' to the variable s. The literal is the constant value; the variable is the container. You can reassign a variable to a different string literal, but the literal itself never changes.

String literals are constants. They exist in your code as written. Variables are names that can hold any string value. When you use a variable name in your code, the interpreter looks up the value it currently holds.

Common Mistakes with Delimiters

  • Mismatching opening and closing delimiters

    The interpreter expects the closing delimiter to match the opening one. A mismatch causes a syntax error because the string is never properly closed.

    Fix: Always use matching delimiters: 'This is a string' or "This is a string"

  • Forgetting to escape a delimiter that appears inside the string

    The interpreter sees the first " after 'said' as the closing delimiter, leaving 'hello" to me"' as unexpected text outside a string.

    Fix: Escape the inner quotes: "She said \"hello\" to me" or use single quotes as the outer delimiter: 'She said "hello" to me'

  • Including an unescaped newline inside a single-line string literal

    Depending on the language, this may cause a syntax error or be interpreted as an escape sequence rather than literal characters.

    Fix: Use the correct escape sequence for your language, or use a multi-line string literal if your language supports it

  • Confusing a string literal with a variable name

    The delimiters create a string literal, so 'name' is the text 'name', not the variable name.

    Fix: Omit the delimiters to reference a variable: print name

Practical Examples with String Literals

Consider a program that checks whether a string starts with a specific prefix. You might write: if name.startswith('Swa'): print 'Yes, the string starts with "Swa"'. Here, 'Swa' is a string literal used as the argument to the startswith method. The outer string 'Yes, the string starts with "Swa"' is another literal, and the inner double quotes are literal characters (not delimiters) because the outer string uses single quotes.

Another common use is the join method, which takes a string literal as a delimiter between items. If you write delimiter = '_*_' and then delimiter.join(['Brazil', 'Russia', 'India', 'China']), the string '_*_' is a literal that acts as a separator. Each element in the list is also a string literal. The result is a single string: Brazil_*_Russia_*_India_*_China.

When to Use Each Delimiter

Choose your delimiter based on the content of your string. If your string contains apostrophes or single quotes, use double quotes as the delimiter. If it contains double quotes, use single quotes. If it contains neither, choose whichever matches your project's style guide or personal preference. This approach minimizes the need for escape characters and makes your code more readable. Only resort to escaping when you must include the same delimiter type that you are using to delimit the string.

Practice: Identifying and Correcting String Literals

MEDIUM

For each of the following code snippets, identify whether the string literal is correctly delimited. If there is an error, explain what is wrong and how to fix it. (1) 'Hello, world!' (2) "She said 'goodbye'" (3) 'It\'s raining' (4) "This is a string' (5) 'The file is named "data.txt"'

Hints
  • Check that opening and closing delimiters match.
  • Look for escape characters before delimiters that appear inside the string.
  • Remember that single and double quotes are interchangeable as long as they match.
  • Consider whether the content inside the delimiters includes the opposite quote type.

Key Takeaways

  • A string literal is a fixed text value written directly in your code, delimited by matching single or double quotes.
  • Delimiters mark the start and end of a string; they must match (both single or both double) for the string to be valid.
  • Single and double quotes are functionally equivalent; choose based on the content of your string to minimize escaping.
  • Use an escape character (backslash) before a delimiter if you need to include that same quote type inside the string.
  • String literals are constants; they differ from variables, which are names that hold string values and can change.