FAQ: Working with Text - String Interpolation

This community-built FAQ covers the “String Interpolation” exercise from the lesson “Working with Text”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise String Interpolation

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

So what does it mean when it’s asking me too Interpolate? Interpolate is used in the Console.WriteLine function and I’m not supposed to use that until step 2. I thought.

4 Likes

Hello, @board3729566001.

Welcome to the forums.

Strings can be printed to the console with the Console.WriteLine() method, but that isn’t the only thing we can do with them. Strings can be stored as values assigned to variables. A string value constructed with interpolation is still a string, and can be assigned to a variable as the instructions ask:
image
For example, if I were going to assign a string to a variable named message using values stored in other variables, I could do this:

string greeting = "Hello";
string name = "Karen";
int cookiesEaten = 7;

//use string interpolation
string message = $"{greeting}, {name}. Today I ate {cookiesEaten} cookies.";

//print message
Console.WriteLine(message);

Output:

Hello, Karen. Today I ate 7 cookies.

2 Likes

I think there needs to be a clearer example of what is being asked for in this section, the right answer requires an additional step that is not given in the example itself.

5 Likes

Take a look at the hint for step 1 of this exercise. This sheds light on the approach they are looking for.

Please…

I have been using this site for months now and I am running into a pattern with the lessons. They start off VERY STRONG, but a few lessons in, I notice that the developers get VERY lazy when it comes to questions in the lesson. In this one SPECIFICALLY, doesn’t clearly tell the student that they want you to include your own “beginning” “middle” and “end” string for 2 lessons.

So, even though we may have been on the right track, that wasn’t after we hit the View Solution button. I hate pressing that button, because it shows that I am not willing to investigate it more. But this is different, I am pressing the View Solution button when I know that I am right, but your instructions are unclear.

It’s very hard to suggest this to others and friends when I know that this is the EXACT issue they will run into with.

Sorry about the post, but I am very passionate about teaching people and I am not for miscommunications on the developers side.

6 Likes

I got stuck on this lesson a little bit because I was using parentheses for example

string story = ($" etc

The Parenthesis was not necessary, but the same exact result was printed to the console.

It is good to know that the Parenthesis are not needed.

1 Like

I’m very stuck on this question. It’s confusing. So, I’m declaring the story string but also interpolating the variables? But there’s no additional linking text between the variables? so am I using the already quoted story text?
Nothing I do seems to work.
Please can I get some actual guidance?

So, I’ve just had to view the solution in the end. The instructions for the exercise are really not clear at all. It’s quite frustrating.

I agree with what the others are saying in this thread. This exercise is lacking proper information on different use cases of interpolation.

Based on the lesson, interpolation is done on the Console.WriteLine() command. There’s no mention that interpolation can be done on different commands.

The lesson should provide multiple different instances where interpolation can be used.

If these lessons are meant for people who have little to no experience with coding, then they need to be a bit more heavy handed.

Also, the whole exercise could of been done by just doing interpolation with Console.WriteLine(), rather than creating a whole separate string variable. The only thing stopping you is the requirements of the exercise itself.

2 Likes

I did the same thing, btw thanks for your post, it really helped!

No. that adds text that is not requested by the prompt.

Hey everyone,

I have an issue understanding why string interpolation is easier to write than string concatenation.
It took me way longer to write the code:
string story=$"{beginning}{middle}{end};
than simply writing it like this->
string story=beginning+middle+end;

I don’t understand the logic very much. Can someone help me? Did i even write the code right?

I think this example doesn’t really show when interpolation would be useful. For writing stories, it’s probably not that great. However, for text where a variable might change, it’s really useful.

For example, let’s say you make a video game where Deb has to collect 5 apples. We start with an int called appleCount, which increases by 1 every time Deb finds an apple. There’s a button you can press to find out how many apples Deb has found.

Without string concatenation, one solution might be to have six lines of code that are dependent on different appleCount values - so if Deb has 0 apples, it would print “Deb has 0 apples!”; if Deb has 1 apple it would print “Deb has 1 apples!” and so on.

This isn’t very efficient code. Instead you could use string concatenation to do something like the following:

string apple = "Deb has {appleCount} apples!"

This is also the case for other use cases - a money tracker might have an interpolated string like "This month you have spent {outgoings} and saved {savings}!" or a location app might have something like "You have checked into this location {visitNumber} times!"

It’s weird that they chose writing a story as the format for teaching this, but I hope my explanation made the logic/reasoning for interpolation clearer!

this excercise is dumb. the theory teaches to interpolate when writing to the console it doesn’t mention anything about interpolating to a new string variable. like come on make it clearer, this is what turns people off

1 Like
  // Declare the variables
  string beginning = "Once upon a time,";
  string middle = "The kid climbed a tree";
  string end = "Everyone lived happily ever after.";

  // Interpolate the string and the variables
  string story = $" {beginning} {middle} {end}";

  // Print the story to the console
  Console.WriteLine(story);

If anyone is confused written above is what your looking to write.
The tutorial was no help figuring out what it wanted you to do.
P.S. Your welcome…

1 Like

I was so confused. I swore I was writing it properly, turns out it needed a space??? before the brackets??

I was stuck for far longer than I had to be. All the examples used were not what they wanted me to enter into the console.

ye iv done same what i dont get is my code was
string story = $“ok so {beginning } timmy the kid {middle} {end} the end”;
now why dont it work just because i put some text before the veriable

string story = $"ok so {beginning } timmy the kid {middle} {end} the end";

Console.WriteLine(story);

Your output:

“ok so Once upon a time, timmy the kid The kid climbed a tree Everyone lived happily ever after. the end”

string story = $"{beginning} {middle}. {end}";

Console.WriteLine(story);

Output:

“Once upon a time, The kid climbed a tree. Everyone lived happily ever after.”

Both strings have valid syntax.

Is your string not displayed on screen OR it is displayed but not accepted by the exercise?

If the latter, then the Codecademy environment most likely expects you to follow the specifications and not improvise upon the expected output.