Python 字符串格式中的“f”和“F”有区别吗?

S.S*_*.S. 10 python string string-formatting

为了格式化 Python 3.6+ 中的字符串,我通常使用小写“f”选项来包含变量。例如:

response = requests.get(f'{base_url}/{endpoint}?fields={field_list}')

我最近看到我的一位同事总是使用大写“F”。像这样:

response = requests.get(F'{base_url}/{endpoint}?fields={field_list}')


小写“f”和大写“F”有区别吗?如果是,您什么时候会使用它们?

Blu*_*sky 10

正如PEP 498规范一章中所解释的,两者都被接受,并且不应有所不同。

In source code, f-strings are string literals that are prefixed by the letter 'f' or 'F'. Everywhere this PEP uses 'f', 'F' may also be used.