Changing to new Python formatting example - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Changing to new Python formatting example (/thread-29827.html) |
Changing to new Python formatting example - leodavinci1990 - Sep-21-2020 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) RE: Changing to new Python formatting example - buran - Sep-21-2020 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 RE: Changing to new Python formatting example - metulburr - Sep-21-2020 no one really uses the old style anymore at all. Some people still use the new method. However f strings are the newest method. RE: Changing to new Python formatting example - yaythomas - Sep-22-2020 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) |