f-strings in Python

DevTechie
DevTechie
Published in
5 min readMay 13, 2024

--

f-strings in Python

Before Python 3.6, there were two basic tools for interpolating (mixing) values, variables, and expressions within string literals — (1) The string interpolation operator (%) (the math’s modulo operator) and (2) The string.format() method. This article talks about the new feature — “f-strings” “f-strings” were introduced in Python 3.6.

f-strings

If you as a developer struggle with string formatting in Python, then do not worry! There are many like you including me. In fact, for most of the developers creating formatted strings is a daunting task. String Formatting is the process of creating dynamic messages which have a mix of strings and values.

To address the issue of string formatting tasks, Python 3.6 introduced a new string formatting style known as “f-strings.” Also known as formatted string literals, f-strings are string literals with f or F before the opening quotation mark.

f-strings can include Python expressions which have replacement fields with curly braces. Python will replace the replacement fields with their corresponding values. This behavior transforms f-strings into a complete string interpolation tool.

This new string formatting method allows you to insert Python expressions inside string literals (constants).

--

--