Chapter 33: Swift Strings: Special Characters
1. What do we mean by “Special Characters” in Swift?
In normal text you write letters, digits, spaces, punctuation — those are ordinary characters.
Special characters are the ones that:
- you cannot type easily on the keyboard
- have a special meaning (like starting an escape sequence)
- need to be escaped (written in a special way) inside a string
The most important special characters fall into two big groups:
Group A — Escape sequences (start with \ backslash)
Group B — Unicode characters (emoji, arrows, symbols, non-English letters, etc.)
2. The Most Important Escape Sequences (you will use these often)
| Escape sequence | What it means | Visual result inside string | Very common real use case |
|---|---|---|---|
| \n | New line (line break) | moves cursor to next line | Multi-line messages, logs, text files |
| \t | Tab (horizontal indentation) | ~4–8 spaces | Aligning columns, pretty-printing tables |
| \” | Literal double quote | “ | Strings that contain quotes |
| \’ | Literal single quote | ‘ | Strings inside single-quoted contexts (rare in Swift) |
| \\ | Literal backslash | \ | File paths (Windows), regex patterns |
| \r | Carriage return | moves cursor to line start | Old text files, some network protocols |
| \0 | Null character | invisible | Rarely used — C interop, low-level strings |
3. Examples – Try these right now
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
// 1. New line \n (most common) let multiLine = "Hello\nWorld\nFrom Hyderabad 🌶️" print(multiLine) // Output: // Hello // World // From Hyderabad 🌶️ |
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 2. Tab \t – very useful for alignment let report = """ Name\tAge\tCity Rahul\t24\tBengaluru Priya\t22\tHyderabad Sneha\t25\tChennai """ print(report) // Output: // Name Age City // Rahul 24 Bengaluru // Priya 22 Hyderabad // Sneha 25 Chennai |
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 3. Quotes inside string let jsonLike = "{\"name\": \"Aarav\", \"age\": 19}" print(jsonLike) // {"name": "Aarav", "age": 19} let dialog = "He said: \"Namaste!\"" print(dialog) // He said: "Namaste!" |
|
0 1 2 3 4 5 6 7 8 9 |
// 4. Backslash itself let windowsPath = "C:\\Users\\aarav\\Documents\\code.swift" print(windowsPath) // C:\Users\aarav\Documents\code.swift |
4. Modern & Recommended Way: Raw Strings (no escaping needed)
Since Swift 5 — this is very popular now.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Old way – lots of backslashes let oldPath = "C:\\Users\\Public\\Documents\\report.pdf" // Modern raw string – clean & readable let rawPath = #"C:\Users\Public\Documents\report.pdf"# print(rawPath) // C:\Users\Public\Documents\report.pdf |
Even better — raw multi-line
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let rawJson = #""" { "name": "Priya", "age": 22, "city": "Hyderabad", "emoji": "😊🚀" } """# print(rawJson) |
When to use raw strings (#”…# or #”””…”””#):
- You have many backslashes (\) → Windows paths, regex, JSON
- You have many double quotes (“) → HTML, JSON, SQL
- You want the string to look exactly as you type it
5. Unicode & Emoji – Swift handles them beautifully
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let emojiString = "नमस्ते 😊🇮🇳🚀🔥" print(emojiString.count) // 8 (counts visible characters) print(emojiString.first!) // न print(emojiString.last!) // 🔥 // Loop over each visible character for char in emojiString { print("→ \(char)") } |
Very common real pattern – status / reaction strings
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let mood = "Happy 😊" let weather = "Hot day 🔥" let location = "Hyderabad 🌶️" let status = "\(mood) – \(weather) in \(location)" print(status) // Happy 😊 – Hot day 🔥 in Hyderabad 🌶️ |
6. Real-Life Examples You Will Actually Write
Example 1 – Log message with timestamp
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
let now = Date() let level = "INFO" let user = "aarav007" let action = "logged in" let logLine = "[\(now)] \(level) user:\(user) action:\(action)" print(logLine) // [2026-02-05 14:30:22 +0000] INFO user:aarav007 action:logged in |
Example 2 – Pretty table (very common in console tools)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let headers = ["Name", "Age", "City"] let row1 = ["Rahul", "24", "Bengaluru"] let row2 = ["Priya", "22", "Hyderabad"] let table = """ \(headers.joined(separator: "\t")) --------------------------- \(row1.joined(separator: "\t")) \(row2.joined(separator: "\t")) """ print(table) |
Example 3 – Dynamic file path (Windows / Unix)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
let username = "aarav" let project = "MyApp" let path = #"\Users\#(username)\Documents\Projects\#(project)\code.swift"# print(path) // \Users\aarav\Documents\Projects\MyApp\code.swift |
7. Quick Reference – Special Characters Cheat Sheet
| What you want to write | How to write it inside String | Raw string way | Most common use case |
|---|---|---|---|
| New line | \n | just press Enter | Logs, multi-line messages |
| Tab | \t | just press Tab key | Tables, aligned output |
| Double quote “ | \” | just write “ | JSON, HTML, SQL |
| Backslash \ | \\ | just write \ | Paths, regex, escape sequences |
| Emoji / Unicode | just paste 😊🇮🇳 | just paste | Status, UI, reactions |
| Any special symbol | just paste ₹ ₹ ₹ | just paste | Currency, symbols, arrows |
8. Small Practice – Try these right now
- Print a 3-line message using \n and include an emoji
- Create a simple aligned table with 3 columns using \t
- Write a JSON-like string that contains double quotes using raw string
- Build a log message that includes date, user name, and action using interpolation
Paste your attempts here if you want feedback or want to improve them!
What would you like to explore next about strings?
- String formatting (numbers, currency, percentages inside strings)
- Splitting strings, joining, trimming, replacing
- Regular expressions (finding patterns in text)
- Working with substrings & string indices in depth
- Strings in SwiftUI (Text, labels, markdown, formatting)
- Or move to another topic (arrays, dictionaries, optionals…)
Just tell me — we’ll continue in the same patient, detailed, teacher-like way 😊
