String Formatting
Built-in function format can be used to
control how strings are displayed.
We saw above the use of built-in function
format for controlling how numerical values are displayed. We now look at how
the format function can be used to control how strings are displayed. As given
above, the format function has the form,
format (value, format_specifier)
where value is the value to be displayed, and format_specifier
can contain a combination of formatting options. For example, to produce the
string 'Hello' left-justified in a field width of 20 characters would be done
as follows,
format('Hello', ' < 20') ➝ 'Hello '
To right-justify the string, the following
would be used,
format('Hello', ' > 20') ➝ ' Hello'
Formatted strings are left-justified by
default. To center the string the '^' character is used:
format('Hello', '^20'). Another use of the
format function is to create strings of blank characters, which is sometimes
useful.
No comments:
Post a Comment