Formatted Text Literals

Revision as of 21:58, 24 March 2021 by Lchrisman (talk | contribs) (Created page with "''New to Analytica 6.0'' A formatted text literal (or F-string for short) is a double-quoted text literal that is prefixed with an F or f. Th...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

New to Analytica 6.0

A formatted text literal (or F-string for short) is a double-quoted text literal that is prefixed with an F or f. These can include replacement expressions that are delineated by curly braces, which get replaced by the result of evaluating the expressions.

The following is an example of an F-String that inserts the current values of x, y and x+y:

F"The value of x is {x}, y is {y} and their sum is {x+y}"

Although you could accomplish the same result using text-concatenation as

"The value of x is " & x & ", y is " & y & " and their sum is " & (x+y)

the version using F-strings is more concise and easier to read.

Formatted Text Literals aren't really literals like normal single- or double-quoted text values are. They are instead themselves expressions whose value is determined at evaluation time. Nevertheless, because they have the syntactic appearance of literals, the term is used. The names Formatted Text Literals and F-Strings are borrowed from Python.

Syntax

A formatted text literal starts with either F" or f", and ends with ". In other words, it is a double-quoted text value prefixed with an upper or lower case F. There can be no space between the F and the quote. Within the text, expressions are inserted using curly braces, {expr}. When the F-String is evaluated, the expressions in curly braces are evaluated and their result is substituted for the curly braces and expression.

There is currently no single-quoted version of F-strings in Analytica, so you cannot write f'Something'. It must use double quotes is in f"Something".

If you want to embed a double quote ("), or curly brace ({ or }) in the text part of an F-string, you must double the character. For example,

 F"An F-string has the form F""some text {{ an expression : format spec }} some more text""."

which has the content: An F-string has the form F"some text { an expression : format spec } some more text".

When the curly braces are not doubled, the content between them is parsed as an expression. Optionally, the expression can be followed by a colon (:) and a format specifier. For example:

F"1/3 = {1/3 : %6}" → "1/3 = 33.333333%"

When you omit a format specifier, the number format for the current expression context is used, i.e., for the current variable or User-Defined Function.

Format Specifiers

Format specifiers are optional, but when you want to specify the format, follow your expression with a colon and then with the format specifiers. F-String format specifiers are terse by design, so as to minimize the distraction from the content and keep your formatted text literal looking as much as possible like the final result. But being terse, they are also cryptic. Except for numeric parts, each element of the format is denoted by a single character.

You don't need a format specifier when you've set the Number Format of your variable or UDF to be as you'd like. Also, you have the option of using NumberToText for a more descriptive way to specify the format.

A format specifier can specify the following information:

  • The number format for numeric values
  • Minimum width (in characters) and alignment within that space
  • The date template for date-time values

All three are optional, or you may specify more than one.

Number formats

A number format specification begins with one of the following characters to specify the format type:

  • 'S' or 's': Suffix
  • 'E' or 'e': Exponential
  • 'G' or 'g': General
  • 'F' or 'f': Fixed point
  • 'I' or 'i': Integer
  • 'P', 'p' or '%': Percent
  • 'B': Boolean (Prints as True or False)
  • 'H' or 'h': Hexadecimal
  • 'b': binary


See Also

Comments


You are not allowed to post comments.