Menu
Coddy logo textTech

String

Part of the Fundamentals section of Coddy's Swift journey — lesson 7 of 86.

A Character is a single character (for example: 1, 6, %, b, p, ., T, etc.)

The String type is a special type that consists of multiple Characters.

To initialize a string value in a variable, enclose it within double quotation marks:

let s1 = "This is a string"
let s2 = "This is also a string"

In the above example, two string constants are initialized, named s1 and s2.

Note: Unlike Python, Swift only uses double quotes " for strings. Single quotes are not valid for strings in Swift.

String Interpolation:

Swift has a powerful feature called string interpolation using \(). This lets you insert values directly inside a string:

let name = "Alice"
let age = 25
print("My name is \(name) and I am \(age) years old.")
// My name is Alice and I am 25 years old.
challenge icon

Challenge

Beginner

Store the string I am learning to code with Coddy! in a constant named coddy.

Be sure to store the exact string value with correct casing.

Cheat sheet

A Character is a single character (e.g., 1, %, b, T).

A String is a type that consists of multiple characters. Strings must be enclosed in double quotation marks:

let s1 = "This is a string"
let s2 = "This is also a string"

Note: Swift only uses double quotes " for strings. Single quotes are not valid.

String Interpolation allows you to insert values directly inside a string using \():

let name = "Alice"
let age = 25
print("My name is \(name) and I am \(age) years old.")
// My name is Alice and I am 25 years old.

Try it yourself

// Type your code below
let coddy = ?

// Don't change the line below
print("coddy = \"\(coddy)\"")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals