f-strings | String Formatting in Python

F-strings, also known as "formatted string literals," are a feature introduced in Python 3.6 that allows you to embed expressions inside string literals. They provide a concise and readable way to create strings that include the values of variables or the results of expressions without the need for concatenation or string formatting functions.

To create an f-string, you simply prefix the string literal with the letter 'f' or 'F'. Inside the f-string, you can include expressions inside curly braces {}. The expressions inside the curly braces are evaluated at runtime and their values are substituted into the final string.

name = "Sherly" age = 30 f_string_example = f"My name is {name} and I am {age} years old." print(f_string_example) #Output: My name is Sherly and I am 30 years old.

Expressions in F-strings

F-strings can include any valid Python expression inside the curly braces. This allows you to perform calculations, access attributes of objects, and call functions directly within the f-string.

num1 = 10 num2 = 5 f_string_expression = f"The sum of {num1} and {num2} is {num1 + num2}." print(f_string_expression) #Output:The sum of 10 and 5 is 15.

Formatting Options

F-strings can also include formatting options to control the display of numeric values, such as specifying the number of decimal places or aligning the values.

price = 19.99 quantity = 3 total = price * quantity f_string_formatting = f"The total cost is ${total:.2f}." print(f_string_formatting) #Output: The total cost is $59.97.

Escaping Curly Braces

If you need to include literal curly braces in the f-string, you can escape them by doubling them ({{ and }}).

product = "Widget" f_string_with_curly_braces = f"{{ {product} }} is a great choice." print(f_string_with_curly_braces) #Output:{ Widget } is a great choice.

f-strings Vs format()


how to python f string

There's a bunch of ways to handle string formatting in Python. f-string strings were introduced to address some of the shortcomings other methods for formatting strings.

3 key reasons why f-Strings are better.

  1. #1 — f-Strings look clean.
  2. #2 — f-Strings are faster.
  3. #3 — f-Strings allow extensive manipulation.

F-Strings, available in Python 3.6 and later versions, are the preferred choice for string formatting due to their concise and efficient nature. If you have the opportunity to work with Python 3.6 or newer, it is recommended to use f-Strings for improved readability and maintainability. However, for scenarios where compatibility with earlier versions is necessary, the format() method remains a viable alternative, ensuring seamless execution across different Python versions while still achieving dynamic string formatting.

Conclusion

F-strings offer a concise and powerful way to create dynamic strings in Python, making code more readable and maintainable. However, please note that f-strings are available in Python 3.6 and above, so if you are using an earlier version, you'll need to use other string formatting methods like str.format() or %.