{"id":2651,"date":"2026-02-05T08:33:39","date_gmt":"2026-02-05T08:33:39","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2651"},"modified":"2026-02-05T08:33:39","modified_gmt":"2026-02-05T08:33:39","slug":"chapter-33-swift-strings-special-characters","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-33-swift-strings-special-characters\/","title":{"rendered":"Chapter 33: Swift Strings: Special Characters"},"content":{"rendered":"<h3 dir=\"auto\">1. What do we mean by \u201cSpecial Characters\u201d in Swift?<\/h3>\n<p dir=\"auto\">In normal text you write letters, digits, spaces, punctuation \u2014 those are <strong>ordinary characters<\/strong>.<\/p>\n<p dir=\"auto\"><strong>Special characters<\/strong> are the ones that:<\/p>\n<ul dir=\"auto\">\n<li>you cannot type easily on the keyboard<\/li>\n<li>have a special meaning (like starting an escape sequence)<\/li>\n<li>need to be <strong>escaped<\/strong> (written in a special way) inside a string<\/li>\n<\/ul>\n<p dir=\"auto\">The most important special characters fall into two big groups:<\/p>\n<p dir=\"auto\"><strong>Group A<\/strong> \u2014 <strong>Escape sequences<\/strong> (start with \\ backslash)<\/p>\n<p dir=\"auto\"><strong>Group B<\/strong> \u2014 <strong>Unicode characters<\/strong> (emoji, arrows, symbols, non-English letters, etc.)<\/p>\n<h3 dir=\"auto\">2. The Most Important Escape Sequences (you will use these often)<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"xs\">Escape sequence<\/th>\n<th data-col-size=\"lg\">What it means<\/th>\n<th data-col-size=\"md\">Visual result inside string<\/th>\n<th data-col-size=\"xl\">Very common real use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xs\">\\n<\/td>\n<td data-col-size=\"lg\">New line (line break)<\/td>\n<td data-col-size=\"md\">moves cursor to next line<\/td>\n<td data-col-size=\"xl\">Multi-line messages, logs, text files<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\\t<\/td>\n<td data-col-size=\"lg\">Tab (horizontal indentation)<\/td>\n<td data-col-size=\"md\">~4\u20138 spaces<\/td>\n<td data-col-size=\"xl\">Aligning columns, pretty-printing tables<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\\&#8221;<\/td>\n<td data-col-size=\"lg\">Literal double quote<\/td>\n<td data-col-size=\"md\">&#8220;<\/td>\n<td data-col-size=\"xl\">Strings that contain quotes<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\\&#8217;<\/td>\n<td data-col-size=\"lg\">Literal single quote<\/td>\n<td data-col-size=\"md\">&#8216;<\/td>\n<td data-col-size=\"xl\">Strings inside single-quoted contexts (rare in Swift)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\\\\<\/td>\n<td data-col-size=\"lg\">Literal backslash<\/td>\n<td data-col-size=\"md\">\\<\/td>\n<td data-col-size=\"xl\">File paths (Windows), regex patterns<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\\r<\/td>\n<td data-col-size=\"lg\">Carriage return<\/td>\n<td data-col-size=\"md\">moves cursor to line start<\/td>\n<td data-col-size=\"xl\">Old text files, some network protocols<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\">\\0<\/td>\n<td data-col-size=\"lg\">Null character<\/td>\n<td data-col-size=\"md\">invisible<\/td>\n<td data-col-size=\"xl\">Rarely used \u2014 C interop, low-level strings<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Examples \u2013 Try these right now<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ 1. New line \\n (most common)\r\nlet multiLine = \"Hello\\nWorld\\nFrom Hyderabad \ud83c\udf36\ufe0f\"\r\nprint(multiLine)\r\n\/\/ Output:\r\n\/\/ Hello\r\n\/\/ World\r\n\/\/ From Hyderabad \ud83c\udf36\ufe0f<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ 2. Tab \\t \u2013 very useful for alignment\r\nlet report = \"\"\"\r\nName\\tAge\\tCity\r\nRahul\\t24\\tBengaluru\r\nPriya\\t22\\tHyderabad\r\nSneha\\t25\\tChennai\r\n\"\"\"\r\n\r\nprint(report)\r\n\/\/ Output:\r\n\/\/ Name    Age     City\r\n\/\/ Rahul   24      Bengaluru\r\n\/\/ Priya   22      Hyderabad\r\n\/\/ Sneha   25      Chennai<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ 3. Quotes inside string\r\nlet jsonLike = \"{\\\"name\\\": \\\"Aarav\\\", \\\"age\\\": 19}\"\r\nprint(jsonLike)\r\n\/\/ {\"name\": \"Aarav\", \"age\": 19}\r\n\r\nlet dialog = \"He said: \\\"Namaste!\\\"\"\r\nprint(dialog)\r\n\/\/ He said: \"Namaste!\"<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ 4. Backslash itself\r\nlet windowsPath = \"C:\\\\Users\\\\aarav\\\\Documents\\\\code.swift\"\r\nprint(windowsPath)\r\n\/\/ C:\\Users\\aarav\\Documents\\code.swift<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. Modern &amp; Recommended Way: Raw Strings (no escaping needed)<\/h3>\n<p dir=\"auto\">Since Swift 5 \u2014 this is <strong>very popular<\/strong> now.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>\/\/ Old way \u2013 lots of backslashes\r\nlet oldPath = \"C:\\\\Users\\\\Public\\\\Documents\\\\report.pdf\"\r\n\r\n\/\/ Modern raw string \u2013 clean &amp; readable\r\nlet rawPath = #\"C:\\Users\\Public\\Documents\\report.pdf\"#\r\n\r\nprint(rawPath)\r\n\/\/ C:\\Users\\Public\\Documents\\report.pdf<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Even better \u2014 raw multi-line<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let rawJson = #\"\"\"\r\n{\r\n    \"name\": \"Priya\",\r\n    \"age\": 22,\r\n    \"city\": \"Hyderabad\",\r\n    \"emoji\": \"\ud83d\ude0a\ud83d\ude80\"\r\n}\r\n\"\"\"#\r\n\r\nprint(rawJson)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>When to use raw strings (#&#8221;\u2026# or #&#8221;&#8221;&#8221;\u2026&#8221;&#8221;&#8221;#):<\/strong><\/p>\n<ul dir=\"auto\">\n<li>You have many backslashes (\\) \u2192 Windows paths, regex, JSON<\/li>\n<li>You have many double quotes (&#8220;) \u2192 HTML, JSON, SQL<\/li>\n<li>You want the string to look <strong>exactly<\/strong> as you type it<\/li>\n<\/ul>\n<h3 dir=\"auto\">5. Unicode &amp; Emoji \u2013 Swift handles them beautifully<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let emojiString = \"\u0928\u092e\u0938\u094d\u0924\u0947 \ud83d\ude0a\ud83c\uddee\ud83c\uddf3\ud83d\ude80\ud83d\udd25\"\r\n\r\nprint(emojiString.count)            \/\/ 8 (counts visible characters)\r\nprint(emojiString.first!)           \/\/ \u0928\r\nprint(emojiString.last!)            \/\/ \ud83d\udd25\r\n\r\n\/\/ Loop over each visible character\r\nfor char in emojiString {\r\n    print(\"\u2192 \\(char)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Very common real pattern \u2013 status \/ reaction strings<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let mood = \"Happy \ud83d\ude0a\"\r\nlet weather = \"Hot day \ud83d\udd25\"\r\nlet location = \"Hyderabad \ud83c\udf36\ufe0f\"\r\n\r\nlet status = \"\\(mood) \u2013 \\(weather) in \\(location)\"\r\nprint(status)   \/\/ Happy \ud83d\ude0a \u2013 Hot day \ud83d\udd25 in Hyderabad \ud83c\udf36\ufe0f<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Real-Life Examples You Will Actually Write<\/h3>\n<h4 dir=\"auto\">Example 1 \u2013 Log message with timestamp<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let now = Date()\r\nlet level = \"INFO\"\r\nlet user = \"aarav007\"\r\nlet action = \"logged in\"\r\n\r\nlet logLine = \"[\\(now)] \\(level) user:\\(user) action:\\(action)\"\r\nprint(logLine)\r\n\/\/ [2026-02-05 14:30:22 +0000] INFO user:aarav007 action:logged in<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 2 \u2013 Pretty table (very common in console tools)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let headers = [\"Name\", \"Age\", \"City\"]\r\nlet row1 = [\"Rahul\", \"24\", \"Bengaluru\"]\r\nlet row2 = [\"Priya\", \"22\", \"Hyderabad\"]\r\n\r\nlet table = \"\"\"\r\n\\(headers.joined(separator: \"\\t\"))\r\n---------------------------\r\n\\(row1.joined(separator: \"\\t\"))\r\n\\(row2.joined(separator: \"\\t\"))\r\n\"\"\"\r\n\r\nprint(table)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Example 3 \u2013 Dynamic file path (Windows \/ Unix)<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>let username = \"aarav\"\r\nlet project = \"MyApp\"\r\n\r\nlet path = #\"\\Users\\#(username)\\Documents\\Projects\\#(project)\\code.swift\"#\r\n\r\nprint(path)\r\n\/\/ \\Users\\aarav\\Documents\\Projects\\MyApp\\code.swift<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Quick Reference \u2013 Special Characters Cheat Sheet<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">What you want to write<\/th>\n<th data-col-size=\"xs\">How to write it inside String<\/th>\n<th data-col-size=\"md\">Raw string way<\/th>\n<th data-col-size=\"lg\">Most common use case<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">New line<\/td>\n<td data-col-size=\"xs\">\\n<\/td>\n<td data-col-size=\"md\">just press Enter<\/td>\n<td data-col-size=\"lg\">Logs, multi-line messages<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Tab<\/td>\n<td data-col-size=\"xs\">\\t<\/td>\n<td data-col-size=\"md\">just press Tab key<\/td>\n<td data-col-size=\"lg\">Tables, aligned output<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Double quote &#8220;<\/td>\n<td data-col-size=\"xs\">\\&#8221;<\/td>\n<td data-col-size=\"md\">just write &#8220;<\/td>\n<td data-col-size=\"lg\">JSON, HTML, SQL<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Backslash \\<\/td>\n<td data-col-size=\"xs\">\\\\<\/td>\n<td data-col-size=\"md\">just write \\<\/td>\n<td data-col-size=\"lg\">Paths, regex, escape sequences<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Emoji \/ Unicode<\/td>\n<td data-col-size=\"xs\">just paste \ud83d\ude0a\ud83c\uddee\ud83c\uddf3<\/td>\n<td data-col-size=\"md\">just paste<\/td>\n<td data-col-size=\"lg\">Status, UI, reactions<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Any special symbol<\/td>\n<td data-col-size=\"xs\">just paste \u20b9 \u20b9 \u20b9<\/td>\n<td data-col-size=\"md\">just paste<\/td>\n<td data-col-size=\"lg\">Currency, symbols, arrows<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Small Practice \u2013 Try these right now<\/h3>\n<ol dir=\"auto\">\n<li>Print a 3-line message using \\n and include an emoji<\/li>\n<li>Create a simple aligned table with 3 columns using \\t<\/li>\n<li>Write a JSON-like string that contains double quotes using raw string<\/li>\n<li>Build a log message that includes date, user name, and action using interpolation<\/li>\n<\/ol>\n<p dir=\"auto\">Paste your attempts here if you want feedback or want to improve them!<\/p>\n<p dir=\"auto\">What would you like to explore next about strings?<\/p>\n<ul dir=\"auto\">\n<li>String <strong>formatting<\/strong> (numbers, currency, percentages inside strings)<\/li>\n<li>Splitting strings, joining, trimming, replacing<\/li>\n<li>Regular expressions (finding patterns in text)<\/li>\n<li>Working with <strong>substrings<\/strong> &amp; string indices in depth<\/li>\n<li>Strings in <strong>SwiftUI<\/strong> (Text, labels, markdown, formatting)<\/li>\n<li>Or move to another topic (arrays, dictionaries, optionals\u2026)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me \u2014 we\u2019ll continue in the same patient, detailed, teacher-like way \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What do we mean by \u201cSpecial Characters\u201d in Swift? In normal text you write letters, digits, spaces, punctuation \u2014 those are ordinary characters. Special characters are the ones that: you cannot type easily&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2651","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/comments?post=2651"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2651\/revisions"}],"predecessor-version":[{"id":2652,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2651\/revisions\/2652"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}