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.
(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

netch80: (Default)
netch80

September 2025

S M T W T F S
 123456
78910111213
1415 1617 181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

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