Python Forum
Changing to new Python formatting example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing to new Python formatting example
#1
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)
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
no one really uses the old style anymore at all. Some people still use the new method. However f strings are the newest method.
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  changing Python files to .exe alok 2 678 Jul-20-2021, 02:49 PM
Last Post: alok
  Changing Index of 2 List in python giddyhead 0 630 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  Automating to run python script 100 times by changing parameters pmt 1 1,299 Dec-29-2020, 10:31 AM
Last Post: andydoc
  changing animation speed using buttons in python microwave 1 911 Jun-24-2020, 05:21 PM
Last Post: GOTO10
  Problem after changing Python directory Souila7 1 968 Jan-25-2020, 05:06 PM
Last Post: ThiefOfTime
  changing the process command line arguments in python Skaperen 3 1,574 Aug-19-2019, 02:40 PM
Last Post: wavic
  Changing Python Versions in Ubuntu 16.04 ErnestTBass 2 1,827 Jan-10-2019, 05:45 PM
Last Post: metulburr
  Python random formatting issues Barnettch3 10 3,521 Jan-28-2018, 11:04 PM
Last Post: Gribouillis
  Python/Tkinter button color changing Crawford3 7 43,106 Aug-10-2017, 02:07 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020