top of page
3DaysOfSwift_final_cleaned_Logo-transparent-v3 ⭐️-tiny.png

Free iOS interview prep - View our homepage & get the full experience

Chapter 3 - Strings and Characters

  • Jun 7
  • 11 min read

The Swift Programming Language Book

Revision Guide


Chapter 3

Strings and Characters



This chapter introduces Swift strings and characters, including string interpolation, Unicode, character counting, substrings and string manipulation.




String Literals


A string literal is a sequence of characters surrounded by quotation marks.

Swift infers that someString is of type String.



Single-Line String Literals


Use double quotation marks to create a string that appears on a single line.

The value of greeting is "Hello, world!".


A single-line string literal cannot contain a line break.



Multi-Line String Literals


Use three double quotation marks to create a string that spans multiple lines.


Everything between the opening and closing delimiters becomes part of the string.


Line breaks are preserved exactly as they appear in the source code.



Special Characters In String Literals


Special characters allow characters that would otherwise be difficult to include within a string literal.


Escape sequences begin with a backslash (\).


When Swift encounters an escape sequence, it interprets it as a special character rather than ordinary text.



Including Quotation Marks


Quotation marks normally mark the beginning and end of a string literal.


Use \" to insert a double quotation mark within a single-line string literal.

The value of wiseWords is:



Including Line Breaks


Use \n to insert a line break within a single-line string literal.

The value of greeting is:

The string contains one line break.



Including Tab Characters


Use \t to insert a horizontal tab.

The tab characters create spacing between the values.



Including Backslashes


Use \\ to include a backslash character within a string.

The value of filePath contains the backslashes as part of the string.



Including A Null Character


Use \0 to insert a null character.

A null character becomes part of the string but is not visible when printed.



Including A Carriage Return


Use \r to insert a carriage return.

Carriage returns are primarily used when working with older text formats and platform-specific data.



Including A Single Quotation Mark


Single quotation marks do not need to be escaped in Swift string literals.

The escape sequence \' does exist for compatibility with other languages and text formats, so you can use it if you need to.


Extended String Delimiters


Extended string delimiters allow quotation marks and backslashes to appear inside a string without escaping them.


Wrap the string in one or more pound signs (#).

The quotation marks become part of the string.


Escape sequences must use the same number of pound signs as the string delimiter.

Extended string delimiters are useful when working with regular expressions, JSON and other text that contains many quotation marks or backslashes.



Multi-Line String Indentation


When using a multi-line string literal, the closing three double quotation marks signify the indentation level.


The code below has no indentation.

Whereas changing the position of the closing three double quotation marks indents each line.


Line Breaks In Multi-Line Strings


A multi-line string preserves the line breaks that appear within the string literal.

The value of quotation contains three lines of text.



Escaping Line Breaks


A backslash (\) at the end of a line prevents the line break from becoming part of the string.

The resulting string contains no line breaks.


This allows long strings to be split across multiple lines without affecting the final value.



Leading And Trailing Line Breaks


A multi-line string can begin or end with line breaks.


Initialising An Empty String


Create an empty string by assigning an empty string literal to a variable.

Swift infers that emptyString is of type String.


An empty string can also be created using the String initializer.

Both declarations create an empty string value.



Checking Whether A String Is Empty


Use the isEmpty property to determine whether a string contains any characters.

This can be read as:

If emptyString contains no characters, print "Nothing to see here"

The isEmpty property returns:


  • true when a string contains no characters.

  • false when a string contains one or more characters.

The result is:

The result is:



String Mutability


A string assigned to a variable can be modified.


A string assigned to a constant cannot be modified.

The value of variableString becomes:

Because variableString was declared using var, its value can change.



Constants And Variables


If a string is assigned to a constant, its value cannot be changed.

The code above is invalid because constantString was declared using let.


Constants prevent accidental modification and help communicate that a value should remain unchanged.



Value Semantics


Strings are value types in Swift.


When a string is assigned to a new variable, the value is copied.

The value of firstName remains:

The value of copiedName becomes:

Changing one string does not affect the other.


Each variable stores its own value.



Working With Characters


A string is a collection of characters.


You can access each character in a string by iterating over it.

The output is:

Each value returned by the loop is of type Character.



Single-Character String Literals


Create a character by assigning a single character to a constant or variable.

The type annotation is required because a single-character string literal is inferred to be a String by default.

Swift infers that letter is of type String.


To create a Character, explicitly specify the type.



Creating A String From Characters


A string can be created from an array of characters.

The value of catString is:

This is useful when characters have been collected or modified individually before being combined into a string.



Concatenating Strings And Characters


A string can be combined with another string or character to create a new string.


This process is known as concatenation.



String Concatenation


Use the addition operator (+) to combine two strings.

The value of welcome is:

Multiple strings can be combined together.

The value of instruction is:


Appending A Character


Use the append() method to add a character to an existing string.

The value of welcome becomes:


Appending A String


A string can also be appended to another string.

The value of welcome becomes:

The += operator combines a string with another string and assigns the result back to the original variable.



Mutability And Appending


Only mutable strings can be modified.

This is valid because greeting was declared using var.

This produces a compiler error because constants cannot be modified.



String Interpolation


String interpolation provides a way to include constants, variables and expressions within a string.


Insert a value into a string by wrapping it in parentheses and preceding it with a backslash.

The value of message is:

String interpolation evaluates each expression and inserts the result into the string.



Interpolating Constants And Variables


A constant can be inserted directly into a string.

The value of greeting is:

Variables can also be interpolated.

The value of result is:


Interpolating Expressions


String interpolation can evaluate expressions.

The value of fruitSummary is:

The expression is evaluated before the result is inserted into the string.



Interpolating Function Results


Function calls can be used within string interpolation.

The value of message is:


Readability


String interpolation is often easier to read than building strings through concatenation.

Compare this to:

Both examples produce the same result.


String interpolation is generally preferred because it is easier to read and maintain.



Unicode


Unicode is an international standard for representing text.


It allows Swift strings to store and display characters from many different languages and writing systems.


For example, a Swift string can contain English text:

Chinese text:

Arabic text:

Or emoji:

All of these values can be stored in a Swift String.



Unicode Scalars


Every character in Unicode is assigned a unique numeric value known as a Unicode scalar.


For example:

The value of dollarSign is:

The Unicode scalar for the dollar sign is:

Unicode scalars can be written using the following syntax:

where n is a hexadecimal value.



What Is A Unicode Scalar?


A Unicode scalar is a unique numeric value assigned to a character by the Unicode standard.


For example, the capital letter A has the Unicode scalar:

The dollar sign has the Unicode scalar:

And the smiling face emoji has the Unicode scalar:

Unicode scalars allow computers to represent characters using numbers rather than storing the characters directly.



Multiple Unicode Scalars


Some visible characters can be created from multiple Unicode scalars.


For example, an accented character can be represented as:

or as:

Both strings display:

Although they use different Unicode scalars internally, they represent the same visible character.



Why Unicode Matters


Unicode allows Swift strings to represent far more than simple ASCII text.


A single Swift string can contain:


  • Letters

  • Numbers

  • Symbols

  • Emoji

  • Characters from many languages


This flexibility is one of the reasons Swift strings behave differently from strings in some other programming languages.



Extended Grapheme Clusters


Swift uses extended grapheme clusters to represent characters.


An extended grapheme cluster is a sequence of one or more Unicode scalars that together produce a single human-readable character.


This allows Swift strings to correctly represent text from many different languages and writing systems.



A Character Can Contain Multiple Unicode Scalars


Some characters can be represented using a single Unicode scalar.


For example:

The character A is represented by a single Unicode scalar.


Other characters can be created from multiple Unicode scalars.

The value of eAcute is:

Although two Unicode scalars were used, Swift treats the result as a single character.



Counting Characters


Because Swift uses extended grapheme clusters, a character count reflects the number of visible characters rather than the number of Unicode scalars.

The value of:

is:

Likewise:

The value of:

is:

Even though the final character may be represented using multiple Unicode scalars, Swift treats it as a single character.



Emoji Characters


Emoji can also be represented using extended grapheme clusters.

The value of:

is:

Swift treats the emoji as a single character.



Family Emoji


Some emoji are composed from multiple Unicode scalars joined together.

The value of:

is:

Although the emoji is built from multiple Unicode components, Swift treats it as a single character.



Why Extended Grapheme Clusters Matter


Extended grapheme clusters allow Swift to count and display characters in the same way that users see them.


A visible character may consist of one Unicode scalar or many Unicode scalars.


Swift treats both cases as a single Character.



Counting Characters


Use the count property to determine the number of characters in a string.

The output is:

The count property returns the number of Character values contained within the string.



Character Counts And Unicode


Because Swift uses extended grapheme clusters, the number of characters in a string is not necessarily the same as the number of Unicode scalars used to create the string.

The value of:

is:

Although the character is represented using multiple Unicode scalars, Swift treats it as a single character.



Emoji And Character Counts


Emoji are counted as characters.

The value of:

is:

Likewise:

The value of:

is:

Although the family emoji is constructed from multiple Unicode components, Swift counts it as a single character.



Empty Strings


An empty string contains zero characters.

The output is:

The count property returns the number of visible characters contained within a string.



Accessing And Modifying A String


Strings are collections of characters.


Unlike arrays, strings cannot be accessed using integer indexes.


Instead, Swift provides a special index type for working with character positions.



String Indices


Every string has a position representing its first character.


Use the startIndex property to access this position.

The value of firstCharacter is:


Accessing Characters After A Given Position


Use the index(after:) method to move to the next character position.

The value of secondCharacter is:

The index(after:) method returns the position immediately following the supplied index.



Accessing Characters Before A Given Position


Use the index(before:) method to move to the previous character position.

The value of lastCharacter is:

The endIndex property represents the position immediately after the final character in the string.



Accessing Characters At An Offset


Use the index(_:offsetBy:) method to move a specific distance from a known position.

The value of character is:

This method allows characters to be accessed further into a string without repeatedly calling index(after:).



Inserting Characters


Use the insert(_:at:) method to insert a character at a specific position.

The value of welcome becomes:


Inserting The Contents Of Another String


Use the insert(contentsOf:at:) method to insert a string at a specific position.

The value of welcome becomes:


Removing Characters


Use the remove(at:) method to remove a character at a specific position.

The value of welcome becomes:


Removing A Range Of Characters


Use the removeSubrange(_:) method to remove multiple characters.

The value of welcome becomes:



Substrings


When part of a string is accessed, Swift creates a Substring.


A substring represents a portion of an existing string.

The value of beginning is:

The type of beginning is:


Why Substrings Exist


Substrings provide a way to work with parts of a string without immediately creating a new string.


This can improve performance when only a portion of a string is required.

The value of welcome is:

The type of welcome is:


Converting A Substring To A String


A substring can be converted into a new string using the String initializer.

The type of string is:


When To Convert A Substring


Substrings are intended for short-term use.


If a portion of text needs to be stored for a longer period of time, convert it to a String.

This creates a new string containing the characters from the substring.



Substrings Behave Like Strings


Most operations that work with strings also work with substrings.

The output is:

Although hello is a Substring, it can still use many of the same methods and properties available on String.



Comparing Strings


Swift provides several ways to compare strings.



String Equality


Use the equality operator (==) to determine whether two strings contain the same characters.

The output is:

Two strings are considered equal when they contain the same characters.



Prefix Equality


Use the hasPrefix(_:) method to determine whether a string begins with a particular sequence of characters.

The value of act1SceneCount is:

The hasPrefix(_:) method returns:


  • true when a string begins with the specified characters.

  • false otherwise.



Suffix Equality


Use the hasSuffix(_:) method to determine whether a string ends with a particular sequence of characters.

The value of mansionCount is:

The hasSuffix(_:) method returns:


  • true when a string ends with the specified characters.

  • false otherwise.



Comparing User Input


String comparison is commonly used when validating user input.

The output is:

String equality, prefix matching and suffix matching are all common techniques when working with text.



Unicode Representations Of Strings


A Swift string can be viewed using different Unicode representations.


These representations expose the underlying Unicode values that make up a string.


Swift provides access to:


  • UTF-8

  • UTF-16

  • Unicode Scalars


Most Swift developers work with strings and characters directly, but these representations are available when lower-level access is required.



UTF-8 Representation


The utf8 property provides access to a string's UTF-8 representation.

You can iterate over the UTF-8 code units:

The output is:

Each value represents part of the string's UTF-8 encoding.



UTF-16 Representation


The utf16 property provides access to a string's UTF-16 representation.

The output is:

Each value represents part of the string's UTF-16 encoding.



Unicode Scalar Representation


The unicodeScalars property provides access to the Unicode scalars contained within a string.

The output is:

Each value represents a Unicode scalar.



Characters Versus Unicode Representations


Most Swift code works with String and Character values.

The value of:

is:

Swift treats the string as five visible characters.


The UTF-8, UTF-16 and Unicode scalar representations expose the lower-level values used to represent those characters.



Choosing The Correct Representation


In most situations, developers should work with strings and characters rather than Unicode representations.


Unicode representations are primarily used when interacting with external systems, file formats and lower-level APIs that require access to encoded text data.





Read the Original Swift.org Chapter

You can find the original and official chapter here - Strings and Characters.





We Converted The Swift Programming Language Book into Xcode Playgrounds

You can download the Xcode playground or view the GitHub repo below.





© 2026 3DaysOfSwift.com. All rights reserved.

Stay Interview Ready.

Don't abandon the technical depth you spent years building.

👩🏿‍💻🧑🏻‍💻🙋🏿‍♀️🧑🏼‍💻👩🏼‍💼👩🏽‍💻🧑🏿‍💻💁🏼‍♀️👩🏼‍💻👨🏼‍💻👨🏽‍💻🙋🏽‍♂️👩🏻‍💻🧑🏾‍💻👩🏻‍💻👩🏾‍💻👨🏼‍💻🙋🏻‍♂️👨🏿‍💻🙋🏼‍♂️


Google Search Terms - SEO Keywords

iOS Developer interview prep. Interview preparation. Revise Swift language features. Swift crash course. How to remind myself of Swift syntax. Swift interview material. How to revise for an iOS interview. iOS interview prep. iOS interview questions. last minute revision for interview. What do I need to do for an iOS interview. Help, I have forgotten how to code in Swift. Swift coding interview prep. Modern iOS interviews. iOS interview platform. Where can I remind myself of how to program Swift? iOS interview platform. What questions do they ask in an iOS interview? Last minute iOS interview revision. Learn Swift. The Swift programming language. learn swift everyday. Apple. iPhone. iOS. iOS Developers. Learn to build apps. Become an iOS Developer. Career building in iOS. Write code in Swift. How to learn the swift computer programming language. How to write code in Xcode? Online course teaching Swift. Online course teaching the Swift programming language. Swift online tutorials. Learn Swift online. Swift.org. Official Swift Language Documentation. https://docs.swift.org/swift-book/documentation/the-swift-programming-language/

 

3DaysOfSwift.com is a professional Swift revision platform built for modern iOS developers who want to remain valuable, technically sharp, and highly employable in an AI-assisted software industry.


We are not affiliated with 100 Days of Swift. If you want to learn SwiftUI please visit HackingWithSwift.com.

Apple developer tutorials for SwiftUI can be found here.

Copyright 2026 www.3DaysOfSwift.com. All rights reserved.



Comments


Commenting on this post isn't available anymore. Contact the site owner for more info.
bottom of page