Types of YAML Strings
YAML supports 4 main styles:
🟢 1. Plain Strings (No quotes)
🟡 2. Single-Quoted Strings ' '
🔵 3. Double-Quoted Strings " "
🟣 4. Multi-line Strings (VERY IMPORTANT 🔥)
🟢 1. Plain Strings (No quotes)
name: Alagarasan
role: DevOps Engineer
company: TCS
🟢 2. Double-Quoted Strings
message: "Start\nEnd" # \n is treated as a newline character
👉 Output
Start
End
quote: "He said, \"YAML is easy\"". # \" is treated as a literal double quote or \" lets you escape double quotes
👉 Output
He said, "YAML is easy"
tab: "Hello\tDevOps" # \t is treated as a tab character
quote: "He said \"Hi\"" # \" is treated as a literal double quote
path: "C:\\Program Files\\App"
👉 Output
C:\Program Files\App
💡 Why?
\ is a special character → must escape as \\
Prevent Boolean Conversion
value: "yes"
👉 Output
yes (string)
Tabs / Formatting # \t = tab spacing
text: "Name:\tAlagarasan"
👉 Output
Name: Alagarasan
Multi-line Formatting (Inline)
msg: "Line1\nLine2\nLine3"
👉 Output
Line1
Line2
Line3
🟢 3. Single-Quoted Strings # Everything is treated literally,No special character processing
In YAML, single-quoted strings (' ') are used when you want the content to be taken literally — no escape sequences, no special processing.
name: 'Alagarasan'
city: 'Chennai'
✔ Everything inside ' ' is treated as plain text.
Special Characters (No escaping needed)
path: '/usr/local/bin'
message: 'Hello: World!'
✔ Characters like :, /, ! are safe inside single quotes.
Quotes Inside String
To include a single quote inside, double it ('')
text: 'It''s DevOps'
👉 Output: It's DevOps. # Only one escape:
No Escape Sequences
newline: 'Line1\nLine2'
👉 Output (literal, NOT new line):
Line1\nLine2
❌ \n is NOT interpreted
✔ It stays as plain text
admin: 'true'
🔍 What’s happening here?
In YAML, certain words are reserved keywords (called boolean values):
true, false
yes, no
on, off
If you write:
admin: true
👉 YAML interprets it as a boolean, not a string.
🧠 Why use single quotes?
When you write:
admin: 'true'
👉 Now YAML treats it as a string, not a boolean.
Value written YAML interprets as
true Boolean true
'true' String "true"
"true" String "true"
admin: 'true' # string
enabled: true # boolean
Leading / Trailing Spaces Preserved
value: ' hello '
👉 Output:
" hello "
When to Use Single Quotes
Use ' ' when:
You want literal values
You don’t need escape sequences
String contains special characters like :, #, !
You want safe, predictable parsing
🟣 4. Multi-line Strings (VERY IMPORTANT 🔥)
Literal block (|) → preserves format
message: |
Hello
DevOps
Team
👉 Output:
Hello
DevOps
Team
Folded block (>) → merges lines
message: >
Hello
DevOps
Team
👉 Output:
Hello DevOps Team
No comments:
Post a Comment