Differences between revisions 4 and 7 (spanning 3 versions)
Revision 4 as of 2019-01-29 04:41:24
Size: 424
Editor: PieterSmit
Comment:
Revision 7 as of 2019-01-29 05:57:35
Size: 679
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
 * numbers {{{  * numbers f'{value:{width}.{precision}f}' {{{
Line 17: Line 17:

>>> f'{75.765367:.0f}'
'76'
Line 18: Line 21:
Line 19: Line 23:
date >>> f'test-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.txt'
'test-2019-01-29_17:53:37.txt'
Line 21: Line 27:
 * unix date time stamp ( seconds since 1970-01-01 ) {{{
f"{datetime.datetime.utcnow():%s}"
}}}

Python Fstring formatting

  • f - string is the latest python string formatting addition, added in 3.5
    • an f-string is created by prefixing the string with a f e.g.

      f"this is a f-string"

Examples of f-strings

  • numbers f'{value:{width}.{precision}f}'

     a= 10.1234
     >>> f'{a:.2f}'
     '10.12'
    
    
    >>> f'{a*1000:,.2f}'
    '10,123.40'
    
    >>> f'{75.765367:.0f}'
    '76'
  • date

    >>> f'test-{datetime.datetime.now():%Y-%m-%d_%H:%M:%S}.txt'
    'test-2019-01-29_17:53:37.txt'
  • unix date time stamp ( seconds since 1970-01-01 )

    f"{datetime.datetime.utcnow():%s}"

Python/FStringsAndFormatting (last edited 2022-11-20 07:49:57 by PieterSmit)