Python 2.6 introduced the str.format()
method with a slightly different syntax from the existing %
operator. Which is better and for what situations?
Python 3.6 has now introduced another string formatting format of string literals (aka "f" strings) via the syntax f"my string"
. Is this formatting option better than the others?
The following uses each method and has the same outcome, so what is the difference?
#!/usr/bin/python sub1 = "python string!" sub2 = "an arg" sub_a = "i am a %s" % sub1 sub_b = "i am a {0}".format(sub1) sub_c = f"i am a {sub1}" arg_a = "with %(kwarg)s!" % {'kwarg':sub2} arg_b = "with {kwarg}!".format(kwarg=sub2) arg_c = f"with {sub2}!" print(sub_a) # "i am a python string!" print(sub_b) # "i am a python string!" print(sub_c) # "i am a python string!" print(arg_a) # "with an arg!" print(arg_b) # "with an arg!" print(arg_c) # "with an arg!"
Furthermore when does string formatting occur in Python? For example, if my logging level is set to HIGH will I still take a hit for performing the following %
operation? And if so, is there a way to avoid this?
log.debug("some debug info: %s" % some_info)
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!
f-strings are faster than both %-formatting and str. format() . At runtime, each expression inside the curly braces gets evaluated within its own scope, and then it's put together into the final string.
There is no difference at all. See the definition of Formatted string literals in the Python documentation. A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'.
The main advantages of using format(…) are that the string can be a bit easier to produce and read as in particular in the second example, and that we don't have to explicitly convert all non-string variables to strings with str(…).
To answer your first question... .format
just seems more sophisticated in many ways. An annoying thing about %
is also how it can either take a variable or a tuple. You'd think the following would always work:
"hi there %s" % name
yet, if name
happens to be (1, 2, 3)
, it will throw a TypeError
. To guarantee that it always prints, you'd need to do
"hi there %s" % (name,) # supply the single argument as a single-item tuple
which is just ugly. .format
doesn't have those issues. Also in the second example you gave, the .format
example is much cleaner looking.
Why would you not use it?
To answer your second question, string formatting happens at the same time as any other operation - when the string formatting expression is evaluated. And Python, not being a lazy language, evaluates expressions before calling functions, so in your log.debug
example, the expression "some debug info: %s"%some_info
will first evaluate to, e.g. "some debug info: roflcopters are active"
, then that string will be passed to log.debug()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With