Python Forum

Full Version: Changing to new Python formatting example
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
1) Is the following changes to new style of Python Formatting correct? And if not, what is a better way to efficiently format by reduce no. of lines, etc.

2) For example, there is the f-string method. When is the best scenario to use each formatting method?

OLD STYLE
return "%s %s\n%s"%(self.salutation,self.name,self.address)
NEW STYLE
return "{} {}\n{}"%(self.salutation,self.name,self.address)
new style
return "{} {}\n{}".format(self.salutation,self.name,self.address)
f-string
return f"{self.salutation} {self.name}\n{self.address}")
use f-strings
no one really uses the old style anymore at all. Some people still use the new method. However f strings are the newest method.
use f-string. they're more readable, and I'm pretty sure a bit more performant (but don't quote me on that).

UNLESS you are in the logger library. In this case, use the old style.

import logging
logger = logging.getLogger('arb')
logger.debug("use old style s% here", your_str_var)