For example, in the expression
'The hex value of {0:d} is {0:02x}'.format(42)
The new, global built-in function 'format' simply calls this special
method, similar to how len()
and str()
simply call their respective
special methods:
def format(value, format_spec):
return value.__format__(format_spec)
The grammar for a replacement field is as follows:
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
arg_name ::= [identifier | integer]
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s"
format_spec ::= <described in the next section>
The format function produces a string by formatting a number of other values according to a specification string. It is similar to the printf function in C, and other similar functions in other programming languages.,Attributes as Blocks - Configuration Language,DocsAbout the DocsIntro to TerraformConfiguration LanguageTerraform CLITerraform CloudTerraform EnterpriseProvider UsePlugin DevelopmentRegistry PublishingIntegration ProgramTerraform ToolsCDK for TerraformGlossary,Configuration Language
format(spec, values...)
format(spec, values...)
format(spec, values...)
> format("Hello, %s!", "Ander")
Hello, Ander!
>
format("There are %d lights", 4)
There are 4 lights
> format("Hello, %s!", "Ander")
Hello, Ander!
>
format("There are %d lights", 4)
There are 4 lights
> format("Hello, %s!", "Ander")
Hello, Ander!
>
format("There are %d lights", 4)
There are 4 lights
> format("Hello, %s!",
var.name)
Hello, Valentina!
>
"Hello, ${var.name}!"
Hello, Valentina!
> format("Hello, %s!",
var.name)
Hello, Valentina!
>
"Hello, ${var.name}!"
Hello, Valentina!
> format("Hello, %s!",
var.name)
Hello, Valentina!
>
"Hello, ${var.name}!"
Hello, Valentina!
Replaces the format item or items in a specified string with the string representation of the corresponding object. A parameter supplies culture-specific formatting information.,Replaces the format items in a string with the string representation of three specified objects. An parameter supplies culture-specific formatting information.,Replaces the format items in a string with the string representation of two specified objects. A parameter supplies culture-specific formatting information.,Replaces the format items in a string with the string representations of corresponding objects in a specified array. A parameter supplies culture-specific formatting information.
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0);
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0);
public static string Format(string format, object arg0);
public static string Format (string format, object arg0);
public static string Format(string format, object ? arg0);
static member Format : string * obj -> string
Public Shared Function Format(format As String, arg0 As Object) As String
The following example uses the Format(String, Object) method to embed an individual's age in the middle of a string.
using namespace System;
void main()
{
DateTime birthdate = DateTime(1993, 7, 28);
array<DateTime>^ dates = gcnew array<DateTime> { DateTime(1993, 8, 16),
DateTime(1994, 7, 28),
DateTime(2000, 10, 16),
DateTime(2003, 7, 27),
DateTime(2007, 5, 27) };
for each (DateTime dateValue in dates)
{
TimeSpan interval = dateValue - birthdate;
// Get the approximate number of years, without accounting for leap years.
int years = ((int)interval.TotalDays) / 365;
// See if adding the number of years exceeds dateValue.
String^ output;
if (birthdate.AddYears(years) <= dateValue) {
output = String::Format("You are now {0} years old.", years);
Console::WriteLine(output);
}
else {
output = String::Format("You are now {0} years old.", years - 1);
Console::WriteLine(output);
}
}
}
// The example displays the following output:
// You are now 0 years old.
// You are now 1 years old.
// You are now 7 years old.
// You are now 9 years old.
// You are now 13 years old.
public:
static System::String ^ Format(System::String ^ format, ... cli::array <System::Object ^> ^ args);
public:
static System::String ^ Format(System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(string format, params object[] args);
public static string Format (string format, params object[] args);
public static string Format(string format, params object ? [] args);
static member Format : string * obj[] -> string
Public Shared Function Format(format As String, ParamArray args As Object()) As String
This example creates a string that contains data on the high and low temperature on a particular date. The composite format string has five format items in the C# example and six in the Visual Basic example. Two of the format items define the width of their corresponding value's string representation, and the first format item also includes a standard date and time format string.
using namespace System;
void main()
{
DateTime date1 = DateTime(2009, 7, 1);
TimeSpan hiTime = TimeSpan(14, 17, 32);
Decimal hiTemp = (Decimal) 62.1;
TimeSpan loTime = TimeSpan(3, 16, 10);
Decimal loTemp = (Decimal)54.8;
String^ result1 = String::Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)",
date1, hiTime, hiTemp, loTime, loTemp);
Console::WriteLine(result1);
Console::WriteLine();
String^ result2 = String::Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)",
gcnew array<Object^> { date1, hiTime, hiTemp, loTime, loTemp });
Console::WriteLine(result2);
}
// The example displays the following output:
// Temperature on 7/1/2009:
// 14:17:32: 62.1 degrees (hi)
// 03:16:10: 54.8 degrees (lo)
// Temperature on 7/1/2009:
// 14:17:32: 62.1 degrees (hi)
// 03:16:10: 54.8 degrees (lo)
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0);
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0);
public static string Format(IFormatProvider provider, string format, object arg0);
public static string Format (IFormatProvider provider, string format, object arg0);
public static string Format(IFormatProvider ? provider, string format, object ? arg0);
static member Format : IFormatProvider * string * obj -> string
Public Shared Function Format(provider As IFormatProvider, format As String, arg0 As Object) As String
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ... cli::array <System::Object ^> ^ args);
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(IFormatProvider provider, string format, params object[] args);
public static string Format (IFormatProvider provider, string format, params object[] args);
public static string Format(IFormatProvider ? provider, string format, params object ? [] args);
static member Format : IFormatProvider * string * obj[] -> string
Public Shared Function Format(provider As IFormatProvider, format As String, ParamArray args As Object()) As String
This example uses the Format(IFormatProvider, String, Object[]) method to display the string representation of some date and time values and numeric values by using several different cultures.
string[] cultureNames = {
"en-US",
"fr-FR",
"de-DE",
"es-ES"
};
DateTime dateToDisplay = new DateTime(2009, 9, 1, 18, 32, 0);
double value = 9164.32;
Console.WriteLine("Culture Date Value\n");
foreach(string cultureName in cultureNames) {
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}",
culture.Name, dateToDisplay, value);
Console.WriteLine(output);
}
// The example displays the following output:
// Culture Date Value
//
// en-US Tuesday, September 01, 2009 9,164.32
// fr-FR mardi 1 septembre 2009 9 164,32
// de-DE Dienstag, 1. September 2009 9.164,32
// es-ES martes, 01 de septiembre de 2009 9.164,32
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format(string format, object arg0, object arg1);
public static string Format (string format, object arg0, object arg1);
public static string Format(string format, object ? arg0, object ? arg1);
static member Format : string * obj * obj -> string
Public Shared Function Format(format As String, arg0 As Object, arg1 As Object) As String
This example uses the Format(String, Object, Object) method to display time and temperature data stored in a generic Dictionary<TKey,TValue> object. Note that the format string has three format items, although there are only two objects to format. This is because the first object in the list (a date and time value) is used by two format items: The first format item displays the time, and the second displays the date.
using namespace System;
using namespace System::Collections::Generic;
void main()
{
Dictionary<DateTime, Double>^ temperatureInfo = gcnew Dictionary<DateTime, Double>();
temperatureInfo->Add(DateTime(2010, 6, 1, 14, 0, 0), 87.46);
temperatureInfo->Add(DateTime(2010, 12, 1, 10, 0, 0), 36.81);
Console::WriteLine("Temperature Information:\n");
String^ output;
for each (KeyValuePair<DateTime, Double>^ item in temperatureInfo)
{
output = String::Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}�F",
item->Key, item->Value);
Console::WriteLine(output);
}
}
// The example displays the following output:
// Temperature Information:
//
// Temperature at 2:00 PM on 6/1/2010: 87.5�F
// Temperature at 10:00 AM on 12/1/2010: 36.8�F
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format(IFormatProvider provider, string format, object arg0, object arg1);
public static string Format (IFormatProvider provider, string format, object arg0, object arg1);
public static string Format(IFormatProvider ? provider, string format, object ? arg0, object ? arg1);
static member Format : IFormatProvider * string * obj * obj -> string
Public Shared Function Format(provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object) As String
By Bernd Klein. Last modified: 29 Jun 2022., 17 Oct 2022 to 21 Oct 2022
q = 459 p = 0.098 print(q, p, p * q)
459 0.098 44.982
print(q, p, p * q, sep = ",")
459, 0.098, 44.982
print(q, p, p * q, sep = " :-) ")
459: -) 0.098: -) 44.982