{"id":2592,"date":"2026-02-02T12:40:10","date_gmt":"2026-02-02T12:40:10","guid":{"rendered":"https:\/\/demo.materiamedica.net\/demo6\/?p=2592"},"modified":"2026-02-02T12:40:10","modified_gmt":"2026-02-02T12:40:10","slug":"chapter-6-print-text","status":"publish","type":"post","link":"https:\/\/demo.materiamedica.net\/demo6\/chapter-6-print-text\/","title":{"rendered":"Chapter 6: Print Text"},"content":{"rendered":"<p dir=\"auto\"><strong>print text in Swift<\/strong> (showing text on the screen \/ in the console).<\/p>\n<p dir=\"auto\">I will explain it slowly, step by step, like we\u2019re sitting together and I\u2019m showing you on my laptop \u2014 with many small, runnable examples, common mistakes, useful tricks, and real-life situations where each method is used.<\/p>\n<p dir=\"auto\">Let\u2019s begin!<\/p>\n<h3 dir=\"auto\">1. The most basic and most used way: print()<\/h3>\n<p dir=\"auto\">This is the <strong>number one function<\/strong> you will use when learning Swift, writing playgrounds, command-line tools, server-side code, or just debugging.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(\"Hello!\")\r\nprint(\"Namaste\")\r\nprint(\"I am learning Swift\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>What you see:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>Hello!\r\nNamaste\r\nI am learning Swift<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Important facts about print():<\/p>\n<ul dir=\"auto\">\n<li>It <strong>automatically adds a new line<\/strong> at the end (\\n)<\/li>\n<li>You can print almost anything: String, Int, Double, Bool, arrays, etc.<\/li>\n<\/ul>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(42)              \/\/ number\r\nprint(3.14159)         \/\/ decimal\r\nprint(true)            \/\/ boolean\r\nprint([\"apple\", \"banana\"])   \/\/ array<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">2. Printing multiple things in one line<\/h3>\n<p dir=\"auto\">You can pass many values to print() \u2014 it puts <strong>a space<\/strong> between them by default.<\/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 name = \"Aarav\"\r\nlet age = 19\r\nlet city = \"Hyderabad\"\r\n\r\nprint(\"Name:\", name, \"Age:\", age, \"City:\", city)<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Output:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>Name: Aarav Age: 19 City: Hyderabad<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">You can control the <strong>separator<\/strong> between items:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(\"Jan\", \"Feb\", \"Mar\", \"Apr\", separator: \" \u2192 \")\r\n\/\/ Jan \u2192 Feb \u2192 Mar \u2192 Apr\r\n\r\nprint(1, 2, 3, 4, 5, separator: \"-\")\r\n\/\/ 1-2-3-4-5<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">3. Controlling the end of the line (no newline, custom ending)<\/h3>\n<p dir=\"auto\">By default print() adds \\n (new line). You can change this with the terminator parameter.<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(\"Loading\", terminator: \"\")\r\nprint(\"...\", terminator: \"\")\r\nprint(\" Done!\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Output (all on one line):<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>Loading... Done!<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Very common pattern \u2014 printing a progress bar or numbers in one line:<\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>for i in 1...10 {\r\n    print(i, terminator: \" \")\r\n}\r\n\r\nprint()   \/\/ just to add final newline<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Output:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>1 2 3 4 5 6 7 8 9 10<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">4. The most important way for real messages: String Interpolation<\/h3>\n<p dir=\"auto\">Almost every real print() uses \\(&#8230;) \u2014 this is called <strong>string interpolation<\/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 username = \"priya_dev\"\r\nlet score = 87\r\nlet level = 12\r\n\r\nprint(\"Welcome back, \\(username)!\")\r\nprint(\"Your current score is \\(score) points.\")\r\nprint(\"You are at level \\(level). Great job!\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">You can put <strong>expressions<\/strong> inside too:<\/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 price = 450\r\nlet quantity = 3\r\n\r\nprint(\"Total amount: \u20b9\\(price * quantity)\")\r\nprint(\"Next year you will be \\(age + 1) years old\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Output:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>Total amount: \u20b91350\r\nNext year you will be 20 years old<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">5. Printing nicely formatted numbers (very important!)<\/h3>\n<p dir=\"auto\">Numbers often look ugly by default:<\/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 bigNumber = 1234567.894\r\nprint(bigNumber)          \/\/ 1234567.894<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Better ways:<\/p>\n<h4 dir=\"auto\">Way 1: Simple fixed decimals with String(format:)<\/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 pi = 3.1415926535\r\nlet money = 9876543.21\r\n\r\nprint(String(format: \"Pi = %.2f\", pi))           \/\/ Pi = 3.14\r\nprint(String(format: \"Amount = %.2f\", money))    \/\/ Amount = 9876543.21<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Way 2: Add thousands separator<\/h4>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(String(format: \"Score: %d\", 1234567))           \/\/ Score: 1234567\r\nprint(String(format: \"Score: %,d\", 1234567))          \/\/ Score: 1,234,567\r\n\r\nprint(String(format: \"Price: \u20b9%,.2f\", 9876543.21))    \/\/ Price: \u20b99,876,543.21<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h4 dir=\"auto\">Way 3: Modern NumberFormatter (clean &amp; flexible)<\/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 formatter = NumberFormatter()\r\nformatter.numberStyle = .decimal\r\nformatter.groupingSeparator = \",\"\r\nformatter.minimumFractionDigits = 2\r\nformatter.maximumFractionDigits = 2\r\n\r\nif let formatted = formatter.string(from: 1234567.89 as NSNumber) {\r\n    print(\"Total: \u20b9\\(formatted)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Output:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>Total: \u20b91,234,567.89<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">6. Printing special characters (emoji, new lines, tabs)<\/h3>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>Swift<\/div>\n<div>\n<pre tabindex=\"0\"><code>print(\"I \u2764\ufe0f Swift\")               \/\/ emoji works perfectly\r\nprint(\"Line 1\\nLine 2\\nLine 3\")   \/\/ \\n = new line\r\n\r\nprint(\"Name\\tAge\\tCity\")          \/\/ \\t = tab\r\nprint(\"Aarav\\t19\\tHyderabad\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Output:<\/strong><\/p>\n<div dir=\"auto\">\n<div data-testid=\"code-block\">\n<div>\n<div>text<\/div>\n<div>\n<pre tabindex=\"0\"><code>I \u2764\ufe0f Swift\r\nLine 1\r\nLine 2\r\nLine 3\r\n\r\nName    Age     City\r\nAarav   19      Hyderabad<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">7. Common beginner mistakes (and how to fix them)<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"lg\">Mistake<\/th>\n<th data-col-size=\"sm\">Wrong code<\/th>\n<th data-col-size=\"lg\">Correct \/ better way<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"lg\">Forgetting quotes<\/td>\n<td data-col-size=\"sm\">print(Hello)<\/td>\n<td data-col-size=\"lg\">print(&#8220;Hello&#8221;)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Using + for many parts<\/td>\n<td data-col-size=\"sm\">print(&#8220;Score: &#8221; + score)<\/td>\n<td data-col-size=\"lg\">print(&#8220;Score:&#8221;, score) or &#8220;\\(score)&#8221;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Too many force-unwraps in print<\/td>\n<td data-col-size=\"sm\">print(optional!)<\/td>\n<td data-col-size=\"lg\">print(optional ?? &#8220;N\/A&#8221;)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Printing without newline when needed<\/td>\n<td data-col-size=\"sm\">print(&#8220;Loading&#8221;) many times<\/td>\n<td data-col-size=\"lg\">Use terminator: &#8221; &#8221; or terminator: &#8220;&#8221;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"lg\">Not formatting money\/numbers<\/td>\n<td data-col-size=\"sm\">print(1234567.8)<\/td>\n<td data-col-size=\"lg\">Use %.2f or NumberFormatter<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">8. Small real-world examples you can copy-paste<\/h3>\n<p dir=\"auto\"><strong>Example 1: Simple user info<\/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 name = \"Sneha\"\r\nlet points = 1420\r\nlet rank = 7\r\n\r\nprint(\"\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\")\r\nprint(\"\u2551          PLAYER INFO         \u2551\")\r\nprint(\"\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563\")\r\nprint(\"\u2551 Name:   \\(name.padding(toLength: 20, withPad: \" \", startingAt: 0)) \u2551\")\r\nprint(\"\u2551 Points: \\(String(format: \"%,d\", points))          \u2551\")\r\nprint(\"\u2551 Rank:   #\\(rank)                    \u2551\")\r\nprint(\"\u255a\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255d\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Example 2: Progress style<\/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>print(\"Downloading files... \", terminator: \"\")\r\nfor _ in 1...5 {\r\n    print(\".\", terminator: \"\")\r\n    \/\/ fake delay\r\n}\r\nprint(\" Done!\")<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>Example 3: List with numbers<\/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 fruits = [\"Mango\", \"Banana\", \"Apple\", \"Orange\", \"Guava\"]\r\n\r\nfor (index, fruit) in fruits.enumerated() {\r\n    print(\"\\(index + 1). \\(fruit)\")\r\n}<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 dir=\"auto\">Summary \u2013 Quick Cheat Sheet<\/h3>\n<div>\n<div dir=\"auto\">\n<table dir=\"auto\">\n<thead>\n<tr>\n<th data-col-size=\"md\">Goal<\/th>\n<th data-col-size=\"lg\">Best code example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"md\">Simple text<\/td>\n<td data-col-size=\"lg\">print(&#8220;Hello&#8221;)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Text + variables<\/td>\n<td data-col-size=\"lg\">print(&#8220;Hi \\(name)&#8221;)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Multiple values<\/td>\n<td data-col-size=\"lg\">print(&#8220;Age:&#8221;, age, &#8220;City:&#8221;, city)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">No newline<\/td>\n<td data-col-size=\"lg\">print(&#8220;Loading&#8221;, terminator: &#8220;&#8221;)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Custom separator<\/td>\n<td data-col-size=\"lg\">`print(a, b, c, separator: &#8220;<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Money \/ decimals<\/td>\n<td data-col-size=\"lg\">String(format: &#8220;\u20b9%.2f&#8221;, amount)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Thousands separator<\/td>\n<td data-col-size=\"lg\">String(format: &#8220;%,d&#8221;, number)<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"md\">Emoji &amp; special chars<\/td>\n<td data-col-size=\"lg\">print(&#8220;I \u2764\ufe0f Swift\\tDone!&#8221;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">Would you like to continue with:<\/p>\n<ul dir=\"auto\">\n<li><strong>Reading input<\/strong> from the user (the opposite of printing)<\/li>\n<li><strong>More advanced formatting<\/strong> (dates, percentages, currencies)<\/li>\n<li><strong>Printing tables<\/strong> or aligned text<\/li>\n<li><strong>Logging<\/strong> instead of print (for real apps)<\/li>\n<li><strong>Printing in colors<\/strong> in terminal (ANSI codes)<\/li>\n<\/ul>\n<p dir=\"auto\">Just tell me what you want to learn next \u2014 we\u2019ll keep going in the same detailed, step-by-step way \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>print text in Swift (showing text on the screen \/ in the console). I will explain it slowly, step by step, like we\u2019re sitting together and I\u2019m showing you on my laptop \u2014 with&#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-2592","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2592","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=2592"}],"version-history":[{"count":1,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2592\/revisions"}],"predecessor-version":[{"id":2593,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/posts\/2592\/revisions\/2593"}],"wp:attachment":[{"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/media?parent=2592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/categories?post=2592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.materiamedica.net\/demo6\/wp-json\/wp\/v2\/tags?post=2592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}