Skip to content
Advertisement

What is print(f”…”)

I am reading through a python script that takes an input of XML files and outputs an XML file. However, I do not understand the printing syntax. Can someone please explain what f in print(f"...") does?

JavaScript

Advertisement

Answer

The f means Formatted string literals and it’s new in Python 3.6.


A formatted string literal or f-string is a string literal that is prefixed with f or F. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.


Some examples of formatted string literals:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement