Formatting strings in Python 3

"(%d goals, $%d)" % (self.goals, self.penalties) 

^ I know how to do this in Python 2

What is the version of Python 3?

I tried to find examples on the Internet, but I kept getting Python 2 versions

+112
python string
Dec 19
source share
4 answers

Here are the syntax documents for the "new" format. An example is:

 "({:d} goals, ${:d})".format(self.goals, self.penalties) 

If both goals and penalties are integers (i.e. their format is approved by default), it can be shortened to:

 "({} goals, ${})".format(self.goals, self.penalties) 

And since parameters are self fields, there is a way to do this using one argument twice (as @Burhan Khalid noted in the comments):

 "({0.goals} goals, ${0.penalties})".format(self) 

Clarification:

  • {} means only the next argument positional with the standard format;
  • {0} means argument with index 0 with standard format;
  • {:d} is the next positional argument with decimal integer format;
  • {0:d} is an argument with index 0 with a decimal integer format.

There are many other things that you can do when choosing an argument (using named arguments instead of positional, access to fields, etc.) and many format options (filling in the number, using thousands separators, displaying the sign or not).). Some other examples:

 "({goals} goals, ${penalties})".format(goals=2, penalties=4) "({goals} goals, ${penalties})".format(**self.__dict__) "first goal: {0.goal_list[0]}".format(self) "second goal: {.goal_list[1]}".format(self) "conversion rate: {:.2f}".format(self.goals / self.shots) # '0.20' "conversion rate: {:.2%}".format(self.goals / self.shots) # '20.45%' "conversion rate: {:.0%}".format(self.goals / self.shots) # '20%' "self: {!s}".format(self) # 'Player: Bob' "self: {!r}".format(self) # '<__main__.Player instance at 0x00BF7260>' "games: {:>3}".format(player1.games) # 'games: 123' "games: {:>3}".format(player2.games) # 'games: 4' "games: {:0>3}".format(player2.games) # 'games: 004' 



Note. . As others noted, the new format does not replace the first, both are available both in Python 3 and in newer versions of Python 2. Some may say that this is a matter of preference, but IMHO is more expressive than the older one and should be used when writing new code (of course, if it is not aimed at older environments).

+170
Dec 19
source share

Python 3.6 now supports string interpolation of strings with PEP 498 . For your use case, the new syntax is simple:

 f"({self.goals} goals, ${self.penalties})" 

This is similar to the previous .format standard, but it makes it easy to do things like :

 >>> width = 10 >>> precision = 4 >>> value = decimal.Decimal('12.34567') >>> f'result: {value:{width}.{precision}}' 'result: 12.35' 
+64
Dec 26 '16 at 22:00
source share

This line works as it is in Python 3.

 >>> sys.version '3.2 (r32:88445, Oct 20 2012, 14:09:29) \n[GCC 4.5.2]' >>> "(%d goals, $%d)" % (self.goals, self.penalties) '(1 goals, $2)' 
+12
Dec 19
source share

It was not out of date; your code is working fine. More details here .

+8
Dec 19
source share



All Articles