Swift string interpolation

So I am on this section of the lessons and I am a bit confused. What is the point of string interpolation when you can just type it out. For example the lesson shows

var albums = 17

print(“Daniel Johnston made (albums) studio albums.”)

With the result being: Daniel Johnston made 17 studio albums.

However when I typed in the editor

var albums = 17

print(“Daniel Johnston made 17 studio albums.”)

I got the same result as when I typed it using string interpolation. Is string interpolation used mainly for more complicated things like adding Int and strings and doubles all into the same line of code???

The second is example is an example of hardcoding. Yes, it will print Daniel Johnston made 17 studio albums, but if you asked for some user input (say, asking them how many albums Daniel made), and printed that to the console, you can’t hardcode that, because you don’t know what the user will input. That’s why you need string interpolation.

In summary, string interpolation is used to print the values in variables, or the values from expressions, etc. Hardcoding is just writing a string (or other value) with a fixed value.

1 Like

Thank you so much for that explanation. Now it makes sense to me.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.