Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f361897

Browse files
authored
Improve f-strings documentation (GH-3604)
Provide additional examples of using format specifiers in f-strings Added examples for using integer and date format specifiers.
1 parent 0ec8c4b commit f361897

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Doc/reference/lexical_analysis.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,12 @@ Some examples of formatted string literals::
676676
>>> value = decimal.Decimal("12.34567")
677677
>>> f"result: {value:{width}.{precision}}" # nested fields
678678
'result: 12.35'
679+
>>> today = datetime(year=2017, month=1, day=27)
680+
>>> f"{today:%b %d, %Y}" # using date format specifier
681+
'January 27, 2017'
682+
>>> number = 1024
683+
>>> f"{number:#0x}" # using integer presentation type as format specifier
684+
'0x400'
679685

680686
A consequence of sharing the same syntax as regular string literals is
681687
that characters in the replacement fields must not conflict with the

0 commit comments

Comments
 (0)