Search code examples
pythonstringoperatorscomputer-science

What does the % inside a string do in Python?


def leakyPipes(n):
if (n > 0):
    if (n % 4 == 0):
        print("drip %d" % n)
        leakyPipes(n-3)
    if (n % 3 == 0):
        print("drop %d" % n)
leakyPipes(12)

What is each of the % in print("drip %d" % n) supposed to do in this situation? The terminal output is giving me this:

drip 12
drop 9
drop 12

Solution

  • It's a formatting specifier. %d would mean digit, %s would mean string, etc etc.