Working with Strings in Swift: Interpolation, Multiline, and Unicode


Every app you use displays text — movie titles, character names, dialogue, error messages. Behind all of that text is a String. Understanding how Swift handles strings unlocks everything from displaying “To infinity and beyond!” to searching, comparing, and transforming text in your apps.

In this guide, you’ll learn how Swift strings work, from basic creation through interpolation, multiline strings, and Unicode. We won’t cover regular expressions or advanced formatting — those deserve their own posts.

What You’ll Learn

What Is a String?

A String is a series of characters — letters, numbers, symbols, and even emoji — grouped together. In Swift, a String is actually a collection of Character values under the hood.

Think of it like the credits at the end of a Pixar movie. Each individual letter on screen is a Character, but the full line “Directed by Pete Docter” is the String. You can look at the whole line, or examine it one character at a time.

let movieTitle = "Toy Story"
let emoji = "🚀"
let empty = ""

All three are valid strings. Even an empty pair of quotes creates a String — it just has zero characters inside.

Apple Docs: String — Swift Standard Library

String Interpolation

String interpolation lets you embed values directly inside a string using \(). This is how you combine text with variables without clunky concatenation.

let character = "Woody"
let movie = "Toy Story"
let year = 1995
let message = "\(character) first appeared in \(movie) (\(year))."
print(message)
Woody first appeared in Toy Story (1995).

Everything inside \() gets converted to text and inserted into the string. You can put variables, math, or even function calls inside the parentheses.

let rating = 8.3
print("Rating: \(rating) out of 10")
print("Double the fun: \(rating * 2)")
Rating: 8.3 out of 10
Double the fun: 16.6

Tip: Prefer string interpolation over the + operator for combining strings with other values. It’s more readable and handles type conversion automatically.

Multiline Strings

When you need a string that spans multiple lines — like a movie synopsis — use triple quotes ("""):

let synopsis = """
    When Woody, the beloved cowboy toy,
    feels threatened by the arrival of
    Buzz Lightyear, he must learn that
    there's room for both of them.
    """
print(synopsis)
When Woody, the beloved cowboy toy,
feels threatened by the arrival of
Buzz Lightyear, he must learn that
there's room for both of them.

The closing """ sets the indentation baseline. Any whitespace to the left of it is stripped from every line, so your code stays neatly indented without extra spaces appearing in the output.

Note: The opening """ must be followed by a line break. The content starts on the next line.

Characters and Counting

Since a String is a collection of Character values, you can loop through each one:

let name = "Buzz"
for character in name {
    print(character)
}
B
u
z
z

To find out how many characters a string has, use the count property:

let catchphrase = "To infinity and beyond!"
print(catchphrase.count)
23

You can also check if a string is empty:

let emptyString = ""
print(emptyString.isEmpty)
true

Tip: Use .isEmpty instead of checking .count == 0. It’s clearer and more efficient.

Why Strings Aren’t Indexed by Integers

If you’ve used other languages, you might expect to write name[0] to get the first character. In Swift, this doesn’t work — and there’s a good reason.

Swift strings support Unicode, which means a single character might be stored using a variable number of bytes. An emoji like ”👨‍👩‍👧‍👦” (family emoji) looks like one character but is actually composed of multiple Unicode scalars joined together.

let family = "👨‍👩‍👧‍👦"
print(family.count)
1

Swift counts this as one Character because it displays as one visible symbol. But because characters can vary in size, Swift can’t just jump to “position 5” like it would in an array of fixed-size elements. Instead, Swift uses special string indices:

let movie = "Ratatouille"
let firstCharacter = movie[movie.startIndex]
print(firstCharacter)
R

To get a character at a specific position, you advance from an existing index:

let movie = "Ratatouille"
let fourthIndex = movie.index(movie.startIndex, offsetBy: 3)
print(movie[fourthIndex])
a

Note: This design ensures correctness with every language and emoji in the world. It’s a trade-off: slightly more verbose code in exchange for text that always works correctly.

Useful String Methods

Swift strings come with many built-in methods. Here are the ones you’ll reach for most often:

let quote = "You've Got a Friend in Me"

print(quote.lowercased())
print(quote.uppercased())
print(quote.contains("Friend"))
print(quote.hasPrefix("You"))
print(quote.hasSuffix("Me"))
you've got a friend in me
YOU'VE GOT A FRIEND IN ME
true
true
true

These methods are case-sensitive — contains("friend") would return false because “Friend” has a capital F.

You can also extract portions of a string using prefix and suffix:

let title = "Finding Nemo"
print(title.prefix(7))
print(title.suffix(4))
Finding
Nemo

Apple Docs: StringProtocol — Swift Standard Library

String Comparison

Comparing strings in Swift uses the == operator, and it’s Unicode-aware:

let a = "café"
let b = "café"
print(a == b)
true

Swift compares the actual characters, not the underlying bytes. Two strings that look the same will compare as equal, even if they were constructed using different Unicode representations.

You can also sort and compare with < and > for alphabetical ordering:

let first = "Buzz"
let second = "Woody"
print(first < second)
true

“Buzz” comes before “Woody” alphabetically, so < returns true.

Common Mistakes

Trying to Index with Integers

// ❌ Don't do this — won't compile
let name = "Woody"
// let first = name[0]
// ✅ Do this instead
let name = "Woody"
let first = name[name.startIndex]
print(first) // W

Swift strings don’t support integer subscripting because of Unicode complexity. Use startIndex, endIndex, and index(_:offsetBy:) instead.

Forgetting Case Sensitivity

// ❌ This returns false
let title = "Monsters, Inc."
print(title.contains("monsters"))
false
// ✅ Convert to the same case first
let title = "Monsters, Inc."
print(title.lowercased().contains("monsters"))
true

When comparing or searching strings, remember that Swift is case-sensitive by default. Convert both sides to the same case when you need case-insensitive matching.

What’s Next?

  • A String is a collection of Character values
  • String interpolation (\()) embeds values inside strings
  • Multiline strings use triple quotes """
  • Swift strings are Unicode-correct — they can’t be indexed by integers
  • Use .contains(), .hasPrefix(), .hasSuffix() for searching
  • String comparison with == is Unicode-aware

Now that you can work with text, it’s time to learn how to store groups of values together. Head over to Swift Collections to learn about arrays, dictionaries, and sets.