← Back to Swift Course | Chapter 2: Variables & Types | Lesson 7 of 8

Strings and Characters

A string is a piece of text, like a word or sentence, and a character is just one single letter or symbol from that text.
Syntax
swift
let name: String = "text"
let letter: Character = "c"
let joined = first + " " + second
let message = "Hi \(name)"
name.count
name.uppercased()

Declaring Strings and Characters

A String holds a sequence of text, while a Character holds exactly one visible character, often used when processing text one symbol at a time.

Example: Declaring Strings and Characters

markup
// Declare a constant `greeting` (a String), set to `"Hello"`
let greeting: String = "Hello"
// Declare a constant `firstLetter` (a Character), set to `"H"`
let firstLetter: Character = "H"
// Print "\(greeting) starts with \(firstLetter)" to the console
print("\(greeting) starts with \(firstLetter)")

Concatenation and Interpolation

Strings can be joined together with the + operator, or embedded into other strings using interpolation with \(expression).

Example: Concatenation and Interpolation

markup
// Declare a constant `first`, set to `"Swift"`
let first = "Swift"
// Declare a constant `second`, set to `"Lang"`
let second = "Lang"
// Declare a constant `combined`, set to `first + " " + second`
let combined = first + " " + second
// Print `combined` to the console
print(combined)
// Print "Combined has \(combined.count) characters" to the console
print("Combined has \(combined.count) characters")

Common String Properties and Methods

Strings expose useful properties like .count, .isEmpty, .uppercased(), and .lowercased() for common text operations.

Example: Common String Properties and Methods

markup
// Declare a constant `message`, set to `"Hello, Swift!"`
let message = "Hello, Swift!"
// Print "Uppercase: \(message.uppercased())" to the console
print("Uppercase: \(message.uppercased())")
// Print "Length: \(message.count)" to the console
print("Length: \(message.count)")
// Print "Is empty: \(message.isEmpty)" to the console
print("Is empty: \(message.isEmpty)")

Iterating Over Characters

Since strings can't be indexed by integer, the idiomatic way to visit each character is to iterate over the string directly in a for-in loop.

Note: This correctly handles Unicode characters that might be built from multiple underlying scalars.

Example: Iterating Over Characters

markup
// Declare a constant `word`, set to `"Swift"`
let word = "Swift"
// Loop over `word`, binding each item to `letter`
for letter in word {
    // Print `letter` to the console
    print(letter)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to index a String with an integer like str[0]; Swift strings use String.Index because of Unicode, not plain integer offsets.
  2. Using + to build a large string in a loop repeatedly, which is less efficient than appending with += or building an array and joining.
  3. Forgetting that a Character can represent a multi-scalar grapheme cluster (like an emoji with a modifier), not just a single simple letter.
Chapter Summary
  • String represents text, and Character represents a single grapheme cluster (visible character).
  • Strings can't be indexed with plain integers; use .first, .last, or String.Index navigation instead.
  • String interpolation (\(value)) and concatenation (+) both build new strings.
  • Swift strings are fully Unicode-correct, which is why simple integer indexing isn't allowed.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.