You are currently viewing F-Strings in Python

F-Strings in Python

The release of Python version 3.6 introduced formatted string literals, simply called “f-strings.” They are called f-strings because you need to prefix a string with the letter ‘f’ in order to get an f-string. The letter ‘f’ also indicates that these strings are used for formatting. Although there are other ways for formatting strings, the Zen of Python states that simple is better than complex and practicality beats purity–and f-strings are really the most simple and practical way for formatting strings.

To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { } characters that can refer to variables or literal values. f-strings support extensive modifiers that control the final appearance of the output string. Expressions in f-strings can be modified by a format specification. Format specifications are used within replacement fields contained within a format string to define how individual values are presented. Each formattable type may define how the format specification is to be interpreted.

Read this definitive guide to using f-strings in Python, courtesy of Prof. Jackie Masloff.