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.
Recommended Tutorials:
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
  Virtual Env changing mysql connection string in python Fredesetes 0 323 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  python-docx: preserve formatting when printing lines Tmagpy 4 2,005 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,539 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  changing Python files to .exe alok 2 2,185 Jul-20-2021, 02:49 PM
Last Post: alok
  Changing Index of 2 List in python giddyhead 0 1,639 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  Automating to run python script 100 times by changing parameters pmt 1 2,562 Dec-29-2020, 10:31 AM
Last Post: andydoc
  changing animation speed using buttons in python microwave 1 2,078 Jun-24-2020, 05:21 PM
Last Post: GOTO10
  Problem after changing Python directory Souila7 1 2,109 Jan-25-2020, 05:06 PM
Last Post: ThiefOfTime
  changing the process command line arguments in python Skaperen 3 2,944 Aug-19-2019, 02:40 PM
Last Post: wavic
  Changing Python Versions in Ubuntu 16.04 ErnestTBass 2 3,089 Jan-10-2019, 05:45 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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