S.S.
S.S.

Reputation: 758

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

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?

Upvotes: 13

Views: 3974

Answers (3)

Khairul Fahim
Khairul Fahim

Reputation: 1

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

Upvotes: 0

chepner
chepner

Reputation: 531718

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.

Upvotes: 3

Blusky
Blusky

Reputation: 3784

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.

Upvotes: 13

Related Questions