Teks sumber — English
Terjemahan — Bahasa Indonesia
For more information on the ``str`` class and its methods, see :ref:`textseq` and the :ref:`string-methods` section below. To output formatted strings, see the :ref:`f-strings` and :ref:`formatstrings` sections. In addition, see the :ref:`stringservices` section.
Formatted String Literals (f-strings)
An :dfn:`f-string` (formally a :dfn:`formatted string literal`) is a string literal that is prefixed with ``f`` or ``F``. This type of string literal allows embedding the results of arbitrary Python expressions within *replacement fields*, which are delimited by curly brackets (``{}``). Each replacement field must contain an expression, optionally followed by:
After the expression has been evaluated, and possibly converted using an explicit conversion specifier, it is formatted using the :func:`format` function. If the replacement field includes a *format specifier* introduced by a colon (``:``), the specifier is passed to :func:`!format` as the second argument. The result of :func:`!format` is then used as the final value for the replacement field. For example::
These strings follow the same syntax and evaluation rules as :ref:`formatted string literals <stdtypes-fstrings>`, with the following differences:
Using :ref:`formatted string literals <f-strings>`, the :meth:`str.format` interface, or :class:`string.Template` may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.
When the right argument is a dictionary (or other mapping type), then the formats in the string *must* include a parenthesised mapping key into that dictionary inserted immediately after the ``'%'`` character. The mapping key selects the value to be formatted from the mapping. For example:
When the right argument is a dictionary (or other mapping type), then the formats in the bytes object *must* include a parenthesised mapping key into that dictionary inserted immediately after the ``'%'`` character. The mapping key selects the value to be formatted from the mapping. For example:
To format only a tuple you should therefore provide a singleton tuple whose only element is the tuple to be formatted.
Untuk memformat hanya *tuple*, Anda harus menyediakan *tuple* *singleton* dimana elemennya hanya *tuple* yang akan diformat.
! formatted string literal
formatted string literals
formatted literal
in formatted string literal