What is f-string in Python?

Starting in Python 3.6, PEP 498 introduced a new string formatting mechanism - Literal String Interpolation (known as f-string). The main idea behind f-strings is to provide a concise and convenient way to embed Python expressions in string literals for formatting. To create an f-string, add the letter "f" or "F" before the opening quotes of your string. To print variable values in the string, you need to specify the variable names inside a set of curly braces "{}" and at the run time, all variable names will be replaced with their values. In this Python f-string example, we add the letter f in front of the string and put the variable names in curly braces. Click Execute to run Python f-string Example online and see the result.
Loading...