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
BeginnerStore 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)\"")This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input