Strings and Characters
Strings in Swift are more than just textβthey're powerful and flexible. One of the most useful features is string interpolation, which lets you embed variables directly into strings:
let name = "Alice"
let greeting = "Hello, \(name)!"
print(greeting) // Hello, Alice!
Need multi-line text? Use triple quotes. This is perfect for longer content without worrying about escape characters:
let poem = """
Roses are red,
Violets are blue,
Swift is awesome,
And so are you.
"""
Strings come with handy methods. Check the length with .count, see if it's empty with .isEmpty, convert to uppercase with .uppercased(), or check if it contains something with .contains().
You can also iterate through each character using a for-in loop, which is great for processing text character by character.
Try it Yourself ->