C.W.K.
Stream
Lesson 03 of 08 · published

Strings — Four Forms, One Rule (Quotes Always)

~10 min · toml, strings

Level 0Plaintext
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

TOML strings always wear quotes

Basic strings — double-quoted, with escapes

"hello\nworld". Same JSON-style escapes (\n \t \\ \" \uXXXX). The default for short strings.

Multi-line basic strings — triple double-quoted

""" line one\nline two """. Escapes work; \ at end of line trims the following whitespace, useful for breaking long lines without inserting a real newline.

Literal strings — single-quoted, no escapes

'C:\Users\Pippa' is exactly those characters. No backslash interpretation, no special meaning. Perfect for Windows paths and regex patterns.

Multi-line literal strings — triple single-quoted

'''line one\nline two'''. Whatever you write, the parser preserves it byte-for-byte (except a leading newline right after the opening fence is trimmed).

Principle: use literal strings (single quotes) for paths, regexes, and Windows-style backslashes — the no-escape rule eliminates an entire class of typos. Use basic strings (double quotes) when you need \n, Unicode escapes, or interpolation. Use multi-line forms when the content is itself multi-line.

Code

All four string forms·toml
basic     = "with \"escapes\" and \u00e9 and \n newline"
literal   = 'C:\Users\Pippa\config.txt'

block_basic = """
This is
multi-line basic. \u00e9 works,
and backslash-newline (\) at end of a line\
folds away.
"""

block_literal = '''
This is multi-line literal.
No escapes. The exact bytes you see
are what the parser delivers.
'''
Windows paths — literal wins·toml
# Painful: every backslash needs doubling
basic_path = "C:\\Users\\Pippa\\.config\\app.toml"

# Cleaner: literal string, no escapes
literal_path = 'C:\Users\Pippa\.config\app.toml'
Line-continuation in multi-line basic·toml
# Without \, the string contains literal newlines:
verbose = """
Line one and line two with a real newline between them.
"""

# With \ at end of line, the newline + leading whitespace are trimmed
folded = """
Line one and line two \
  joined as one logical line.
"""
# folded == "Line one and line two joined as one logical line.\n"

External links

Exercise

Write four config values: a Windows path, a regex pattern with backslashes, a multi-line shell script, and a one-line greeting with a Unicode emoji escape. Use the right string form for each. Parse with tomllib/tomli and confirm each value matches your intent.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.