src/pylib/pystring/fstring

Source   Edit  

f-string

a string literal with a prefix of fr, Fr, rf of Rf behaves as if with a prefix of strformat.fmt.

and f"xxx" is different, where the escaped literals in string will be interpreted.

Example:

import src/pylib/pystring/fstring
assert fr"\n" == "\\n"
assert f"\n" == "\n"

let s = "asd"

assert f"[{s}]" == "[asd]"
assert not compiles(fr s)

Macros

macro Fr(pattern: string{lit}): PyStr
Source   Edit  
macro fr(pattern: string{lit}): PyStr
Source   Edit  
macro rf(pattern: string{lit}): PyStr
Source   Edit  
macro Rf(pattern: string{lit}): PyStr
Source   Edit  

Templates

template f(s: string{lit}): PyStr

Python F-String.

Not the same as Nim's fmt"xxx" as that's equal to fmt r"xxx", a.k.a fr"xx" in Python

Any escape-translation error is reported at compile-time, with information of filename and line number

Unicode

\Uhhhhhhhh is supported as Python's, while Nim's \U{...} is unsupported but \u{...} is reserved

oct

\\[0-7]{1,3} in f-string will be interpreted as octal digit as Python,

instead of decimal as Nim.

multiline

the following shows the deature of Nim's multiline string which is different from Python's

assert "" == """
"""

## Unicode Names
`\N{name}` is not supported yet.

Example:

assert f"\n" == "\n"
assert f"123{'a'}\n456" == "123a\n456"

assert f"\U0001f451" == "👑"

assert f"\10" == "\x08"   # nim's "\10" means chr(10)

assert f"""\t123
""" == "\t123\n"  # even if the source code's newline is crlf.
Source   Edit