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.

Date: 2025-03-13 09:45 am (UTC)
gegmopo4: (Default)
From: [personal profile] gegmopo4

\".12e\" is not valid Python syntax. But ".12e" is, and in Python 3.12 you can use the same inner and outer quotes: ```

f"{math.pi:{".12e"}}" '3.141592653590e+00' You can also use a backslash in nested expressions in f-string (this was the main motivation of implementing a new parser for f-strings): f"{"\n".join("abc")}" 'a\nb\nc' ```

Date: 2025-03-13 09:53 am (UTC)
gegmopo4: (Default)
From: [personal profile] gegmopo4

Dreamwidth and GitHub use different flavors of Markdown.

Date: 2025-03-13 12:33 pm (UTC)
vitus_wagner: My photo 2005 (Default)
From: [personal profile] vitus_wagner

Чем хорош маркдаун, так это обилием стандартов. Выбирай, не хочу.

Profile

netch80: (Default)
netch80

September 2025

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

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

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