YAML Strings
A string is text: a name, a message, a city, or a file path. YAML lets you write strings in three ways. Each way suits a different situation, and knowing the differences prevents surprising results.
Plain Strings
A plain string needs no quotes. Type the text after the colon and the parser reads it as text.
city: Delhi
greeting: Hello world
path: /home/user/filesPlain strings are the cleanest option. Spaces inside the text are fine. Leading and trailing spaces disappear.
Single-Quoted Strings
Single quotes keep every character exactly as typed. Backslashes have no special meaning inside them.
folder: 'C:\Users\Asha'
note: 'It''s ready'To place an apostrophe inside single quotes, type it twice. The second line above produces the text It's ready.
Double-Quoted Strings
Double quotes understand escape sequences. A backslash followed by a letter creates a special character.
message: "Line one\nLine two"
price: "Cost: \u20B950"
quote: "She said \"hi\""| Escape | Meaning |
|---|---|
| \n | New line |
| \t | Tab character |
| \" | Double quote |
| \\ | Backslash |
| \u20B9 | Unicode character by code |
Choosing the Right Style
Does the text contain special symbols or look like a number?
|
+----+----+
| No | Yes
v v
Plain Need escapes like \n ?
string |
+----+----+
| No | Yes
v v
Single Double
quotes quotes
Start with plain strings. Add quotes only when the parser would misread the text.
When Quotes Become Necessary
Certain patterns confuse the parser. Wrap the value in quotes when it contains one of these.
- A colon followed by a space, such as
time: 10: 30. - A hash sign preceded by a space, which starts a comment.
- A first character from this set:
* & ! % @ ` { [ | >. - A value that looks like a number, boolean, or null, when you want text.
title: "Chapter 1: Basics"
code: "007"
answer: "yes"
tag: "#python"Without the quotes, 007 would become the number 7 in some parsers, and yes would turn into a boolean in older YAML versions.
Empty Strings
Two quote marks with nothing between them create an empty string:
middle_name: ""An empty string differs from a missing value. A later topic on null values explains the distinction.
Strings That Look Like Other Types
| Written as | Parsed as | Fix to force text |
|---|---|---|
| 42 | Number | "42" |
| true | Boolean | "true" |
| null | Null | "null" |
| 2026-10-22 | Date in many parsers | "2026-10-22" |
| 1.10 | Float 1.1 | "1.10" |
Long Strings Across Lines
Plain and quoted strings can wrap onto the next lines. The parser folds the line breaks into single spaces:
description: This sentence is long
and continues on another line.The result reads as one sentence with a space where the break occurred. Block styles for longer text appear in the multiline strings topic.
Practical Advice
- Quote version numbers such as "3.10" to protect the trailing zero.
- Wrap values that start with zero, like postal codes or phone numbers.
- Use double quotes when the text needs a new line or a Unicode symbol.
- Single quotes suit Windows paths and regular expressions with many backslashes.
Strings in Lists and Mappings
Strings appear as list items and as keys too. The quoting rules stay identical in every position. A list of city names can mix plain and quoted entries, and the parser returns all of them as text.
Mixed quoting inside one list works fine, yet a single style per list keeps the file tidy. Readers then spot the odd entry that needs special care, such as a name containing a colon.
Consistent quoting makes a file predictable. Teams often agree on one rule, such as quoting every value that is not obviously plain text.
