String can be formatted to accept a padding parameter that will specify how many character positions the inserted string will use :
${value, padding}
NOTE: Positive padding values indicate left padding and negative padding values indicate right padding.
A left padding of 5 (adds 3 spaces before the value of number, so it takes up a total of 5 character positions in the resulting string.)
var number = 42;
var str = $"The answer to life, the universe and everything is {number, 5}.";
//str is "The answer to life, the universe and everything is 42.";
// ^^^^^
System.Console.WriteLine(str);
Output:
The answer to life, the universe and everything is 42.
Right padding, which uses a negative padding value, will add spaces to the end of the current value.
var number = 42;
var str = $"The answer to life, the universe and everything is ${number, -5}.";
//str is "The answer to life, the universe and everything is 42 .";
// ^^^^^
System.Console.WriteLine(str);
Output:
The answer to life, the universe and everything is 42 .
You can also use existing formatting specifiers in conjunction with padding.
var number = 42;
var str = $"The answer to life, the universe and everything is ${number, 5:f1}";
//str is "The answer to life, the universe and everything is 42.1 ";
// ^^^^^