4

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

Original question: String Formatting in Python 3

Python3 Format String Syntax Ref: https://docs.python.org/3/library/string.html#format-string-syntax

Community
  • 1
  • 1
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • 2
    Did you consider just trying the expression? `"(%d goals, $%d)" % (42, 42)` immediately shows me that the `$` sign goes straight to the output string. – glglgl Dec 05 '14 at 15:41
  • 3
    Upvoted. This is a perfectly legitimate question, and if I was a new user, I'd suspect that `$%` was some operator. We must try to be helpful and respectful to new users. (I remember when I used to do PERL and dabbled in PHP, it was like getting slowly beaten to death by the ASCII character set) – smci Mar 11 '16 at 15:57
  • While the string formatting of python 3 does not use `$` anymore, it can have meaning if passing a string argument to `matplotlib` or other functions. So make sure and check the context of the string as well. – johnDanger Jan 07 '20 at 21:51

1 Answers1

13

It has no special meaning. It just inserts a $ character:

>>> "(%d goals, $%d)" % (10, 42)
'(10 goals, $42)'

Sometimes a dollar is just a dollar.

You also linked to the wrong documentation; the formatting syntax documented there only applies to the format() function and the str.format() method. You want to look at the printf-style String Formatting section instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343