Is there a difference between "f" and "F" in Python string formatting?

Viewed 1475

To format strings in Python 3.6+, I usually use the lowercase "f" option to include variables. For example:

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

I've recently seen one of my coworkers who always uses capital "F", instead. Like this:

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


Is there a difference between the lowercase "f" and capital "F"? And if yes, when would you use each?

3 Answers

As explained in the PEP 498, chapter Specification, both are accepted, and should not differ.

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.

There is no difference at all. See the definition of Formatted string literals in the Python documentation.

A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'.

No further mention is made of the specific character used to introduce the literal.

There is no difference between the two. For better understanding, you may go through string formatting.

Related