# String Interpolation

Build strings by putting values straight into the text with $"...{value}...", and format numbers, money and columns as you go.

Novice · C# · Updated November 3, 2026

## After this lesson you can

- Build a string with interpolation, including format specifiers

## Key takeaways

- Put `$` before the opening quote, and C# works out anything in curly braces and puts it into the text, as in `$"({rank}) {teamName}"`.
- The braces can hold any expression, not only a variable; an expression with `?:` needs parentheses around it.
- A format specifier after a colon sets how a number looks: `F2` for 2 decimal places, `N0` for thousands separators, `C` for currency and `P0` for a percentage.
- A comma and a width line up columns: a positive width right-aligns, a negative width left-aligns.
- To print a curly brace itself, double it: `{{` prints `{` and `}}` prints `}`.

So far you've built strings by joining pieces with `+`. It works, but with several values the quotes and plus signs pile up. **String interpolation** lets you write the text once, with the values in place.

## $ and curly braces

Put `$` before the opening quote. Then, inside the string, anything in curly braces is worked out and put into the text:

```csharp
string teamName = "Georgia";
int rank = 1;
int finalScore = 39;

Console.WriteLine("(" + rank + ") " + teamName + ": " + finalScore);
Console.WriteLine($"({rank}) {teamName}: {finalScore}");
```

```text
(1) Georgia: 39
(1) Georgia: 39
```

Both lines print the same thing, but the second reads like the output it makes. From now on, this course uses interpolation.

The braces can hold any expression, not only a variable:

```csharp
int wins = 7;
int losses = 3;

Console.WriteLine($"Games played: {wins + losses}");
Console.WriteLine($"Winning percentage: {wins * 100 / (wins + losses)}%");
Console.WriteLine($"Result: {(wins > losses ? "winning record" : "rebuilding")}");
```

```text
Games played: 10
Winning percentage: 70%
Result: winning record
```

An expression with `?:` inside the braces needs parentheses around it, because `:` has a special meaning there.

## Formatting numbers

After the value, a colon and a **format specifier** say how it should look:

| Specifier | Means | Example | Result |
|---|---|---|---|
| `F2` | 2 decimal places | `{3.14159:F2}` | `3.14` |
| `N0` | thousands separators, no decimals | `{1234567:N0}` | `1,234,567` |
| `C` | currency | `{12.5m:C}` | `$12.50` |
| `P0` | a percentage, no decimals | `{0.75:P0}` | `75%` |

```csharp
decimal price = 12.5m;
double average = 24.5678;
int attendance = 92746;

Console.WriteLine($"Price: {price:C}");
Console.WriteLine($"Average: {average:F2}");
Console.WriteLine($"Attendance: {attendance:N0}");
```

```text
Price: $12.50
Average: 24.57
Attendance: 92,746
```

`C`, `N0` and `P0` follow your computer's region settings: a computer set up for the United States shows dollars and commas, while one set up for Germany shows `12,50 €`.

## Lining up columns

A comma and a number set a minimum width: positive pads on the left (right-aligns), negative pads on the right (left-aligns). It's handy for tables:

```csharp
Console.WriteLine($"{"Team",-12}{"Final",6}");
Console.WriteLine($"{"Georgia",-12}{39,6}");
Console.WriteLine($"{"Ohio State",-12}{52,6}");
```

```text
Team         Final
Georgia         39
Ohio State      52
```

## Braces in the text

To print a curly brace itself, double it: `{{` prints `{`, and `}}` prints `}`.

```csharp
string tag = "p";
Console.WriteLine($"CSS: {tag} {{ color: navy; }}");
```

```text
CSS: p { color: navy; }
```

## Check yourself

2 questions to try on the lesson page.

## So what

Interpolation keeps the shape of the output visible in your code, and format specifiers handle money, decimals and columns without any extra arithmetic.

## Key terms

- [String Interpolation](https://www.ask-codey.com/glossary/string-interpolation-c-sharp)
- [Format Specifier](https://www.ask-codey.com/glossary/format-specifier)

## Cheat sheets

- [String Interpolation](https://www.ask-codey.com/cheat-sheets/strings-in-csharp-string-interpolation-cheat-sheet)

Source: https://www.ask-codey.com/lessons/strings-in-csharp-string-interpolation
