Search code examples
pythonpython-3.xstring-formatting

What is the purpose of dollar sign in this Python3 string formatting expression?


"(%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


Solution

  • 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.