Swift
Programming
New Line
Swift Tutorial
Coding Basics

How do I make a new line in swift

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, use the escape sequence \n inside a string to create a new line. This is the standard newline character used across all Apple platforms (iOS, macOS, watchOS, tvOS). Swift also supports multi-line string literals using triple quotes ("""), which preserve line breaks as written in source code. For building strings dynamically, \n can be inserted through string interpolation, concatenation, or appended to a mutable string.

The \n Escape Sequence

swift
1let greeting = "Hello\nWorld"
2print(greeting)
3// Output:
4// Hello
5// World

\n is the newline character (Unicode U+000A). When Swift encounters \n inside a string literal, it outputs a line break at that position.

swift
1// Multiple new lines
2let text = "Line 1\nLine 2\nLine 3"
3print(text)
4// Line 1
5// Line 2
6// Line 3
7
8// Blank line between content
9let spaced = "Header\n\nBody text"
10print(spaced)
11// Header
12//
13// Body text

Multi-Line String Literals

Swift's triple-quote syntax lets you write multi-line strings without \n:

swift
1let poem = """
2    Roses are red,
3    Violets are blue,
4    Swift is great,
5    And so are you.
6    """
7print(poem)
8// Roses are red,
9// Violets are blue,
10// Swift is great,
11// And so are you.

The closing """ indentation determines how much leading whitespace is stripped. Content indented beyond the closing quotes keeps its extra indentation:

swift
1let code = """
2    func hello() {
3        print("Hi")
4    }
5    """
6// func hello() {
7//     print("Hi")
8// }

Continuation Lines

To break a long line in source code without inserting a newline in the output, end the line with a backslash:

swift
1let longSentence = """
2    This is a very long sentence that \
3    continues on the same line in output.
4    """
5print(longSentence)
6// This is a very long sentence that continues on the same line in output.

String Interpolation with New Lines

swift
1let name = "Alice"
2let age = 30
3let profile = "Name: \(name)\nAge: \(age)"
4print(profile)
5// Name: Alice
6// Age: 30

Building Strings with New Lines

Concatenation

swift
1let header = "Title"
2let body = "Content here"
3let combined = header + "\n" + body
4print(combined)
5// Title
6// Content here

Appending to a Mutable String

swift
1var log = ""
2log += "Step 1: Started\n"
3log += "Step 2: Processing\n"
4log += "Step 3: Done\n"
5print(log)
6// Step 1: Started
7// Step 2: Processing
8// Step 3: Done

Joining an Array

swift
1let lines = ["Apple", "Banana", "Cherry"]
2let joined = lines.joined(separator: "\n")
3print(joined)
4// Apple
5// Banana
6// Cherry

joined(separator:) inserts the separator between each element — useful for building multi-line output from collections.

Platform-Specific Line Endings

swift
1// Unix/macOS/iOS (standard)
2let unix = "Line 1\nLine 2"       // \n (LF)
3
4// Windows-style
5let windows = "Line 1\r\nLine 2"  // \r\n (CRLF)
6
7// Classic Mac (pre-OS X, rare)
8let classicMac = "Line 1\rLine 2" // \r (CR)

On Apple platforms, always use \n. The \r\n combination is only needed when generating text for Windows systems or certain network protocols (HTTP, SMTP, FTP).

New Lines in SwiftUI

swift
1import SwiftUI
2
3struct ContentView: View {
4    var body: some View {
5        VStack {
6            // \n works in Text
7            Text("Hello\nWorld")
8
9            // Multi-line string in Text
10            Text("""
11            First line
12            Second line
13            Third line
14            """)
15
16            // Or use multiple Text views
17            VStack(alignment: .leading) {
18                Text("Line 1")
19                Text("Line 2")
20                Text("Line 3")
21            }
22        }
23    }
24}

In SwiftUI, Text views render \n as line breaks. For more control over spacing, use a VStack with separate Text views and .padding() or .spacing.

Special Characters Reference

swift
1let escapes = """
2    \\n  → newline (line feed)
3    \\r  → carriage return
4    \\t  → tab
5    \\\\  → backslash
6    \\"  → double quote
7    \\0  → null character
8    """

All escape sequences use a backslash prefix. Inside multi-line string literals ("""), double quotes do not need escaping unless three appear consecutively.

Common Pitfalls

  • Raw strings ignore escape sequences: #"Hello\nWorld"# prints literally Hello\nWorld — the \n is not interpreted. To use escape sequences in a raw string, add matching pound signs: #"Hello\#nWorld"#.
  • Trailing newline in multi-line strings: The closing """ on its own line adds a final newline. Place it on the same line as the last text to avoid it: """last line""".
  • Windows line endings in files: If you read a file created on Windows, it may contain \r\n. Use components(separatedBy: .newlines) instead of split(separator: "\n") to handle both \n and \r\n transparently.
  • Whitespace in multi-line strings: The indentation of the closing """ defines the baseline. Content must be indented at least as far as the closing quotes, or the compiler reports an error.
  • print() adds its own newline: print("Hello") outputs Hello\n — it appends a newline by default. To suppress it, use print("Hello", terminator: "").

Summary

  • Use \n in string literals for newlines: "Line 1\nLine 2"
  • Use triple-quote """ for multi-line strings that preserve source formatting
  • Use joined(separator: "\n") to combine array elements with line breaks
  • print() automatically appends a newline — use terminator: "" to suppress it
  • In SwiftUI, Text("Line 1\nLine 2") renders line breaks as expected
  • On Apple platforms, always use \n (LF) — use \r\n only for Windows interop

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.