Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTimothy Poole
122 PointsHelp with interpolated string
Not fully understanding the second part of this question when it asks to make it an interpolated string after declaring the constant greeting. Please help!
// Enter your code below
let name = "TJ"
let greeting = "Hi there,"
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Timothy,
Swift has two main mechanisms for combining strings: concatenation and interpolation.
Here is a simple example of each. First, concatenation:
let first_string = "hello, "
let second_string = "world."
let concatenated = first_string + second_string // note the use of the '+' operator
Second, interpolation:
let name = "timothy"
let greeting = "hello \(name)" // note that swift evaluates any code inside '\()'
Interpolation is usually used to put a value from one variable or constant inside another, but it is much more powerful than that, and can be used to evaluate any code. For example:
let a = 1
let b = 2
let math = "\(a) + \(b) is equal to \(a + b)" // observe that the math is being done inside the \()
Hope that's clear
Alex
Timothy Poole
122 PointsThank you for getting back and have such a detailed explanation!
joe Bell Fenner
Courses Plus Student 2,646 Pointsjoe Bell Fenner
Courses Plus Student 2,646 Pointsyou have got name all you need to do is add a backslash(name) next to "Hi there," inside the double quotes.
let greeting = "Hi there, a backslash (name)"
"backslash ()" this is known as an interpolation.
good luck bud.