Skip to content

PEP498 Standard: Formatting #8

@rinslow

Description

@rinslow
>>> value = 1234
>>> f'input={value:#06x}'
'input=0x04d2'
f'{date} was on a {date:%A}'
'1991-10-12 was on a Saturday'

Format specifiers may also contain evaluated expressions. This allows code such as:

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal('12.34567')
>>> f'result: {value:{width}.{precision}}'
'result:      12.35'

Implement this in fstring!

Hint:

f'abc{expr1:spec1}{expr2!r:spec2}def{expr3}ghi'

Might be evaluated as:

'abc' + format(expr1, spec1) + format(repr(expr2), spec2) + 'def' + format(expr3) + 'ghi'

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions