netch80: (Default)
[personal profile] netch80
Seems apparent, but only after occasionally found this.


>>> "{}".format(math.pi)
'3.141592653589793'
>>> "{:{}}".format(math.pi, 'f')
'3.141593'
>>> "{:{}}".format(math.pi, '.14f')
'3.14159265358979'

>>> f"{math.pi}"
'3.141592653589793'
>>> pr=10
>>> f"{math.pi:.{pr}e}"
'3.1415926536e+00'


And even:


>>> "{:{}}".format(math.pi, '.16e')
'3.1415926535897931e+00'
>>> f"{math.pi:{'.12e'}}"
'3.141592653590e+00'


But this way isnʼt allowed:


>>> f"{math.pi:{\".12e\"}}"
  File "", line 1
    f"{math.pi:{\".12e\"}}"
                           ^
SyntaxError: f-string expression part cannot include a backslash


but we can override it with a trick:


>>> f"{math.pi:{'.12e'}}"
'3.141592653590e+00'
>>> f"""{math.pi:{".12e"}}"""
'3.141592653590e+00'


Not an often case but still useful.

Profile

netch80: (Default)
netch80

January 2026

S M T W T F S
    1 23
45678910
11121314151617
18192021222324
25262728293031

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 2nd, 2026 08:32 am
Powered by Dreamwidth Studios