← All questions
MediumProgramming

What does Python print for fancy(3, '.') when default arguments are used?

TL;DR
It prints .*.*.*..

Question

What does this code print?

def fancy(length, char1='-', char2='*'):
    return (char1 + char2) * length + char1

print(fancy(3, '.'))

Explanation

In fancy(3, '.'), length receives 3, char1 receives a full stop, and char2 keeps its default value, an asterisk.

The pair .* is repeated three times, giving .*.*.*. A final full stop from char1 is appended, producing .*.*.*..

Default argumentsPythonStrings

Platforms

  • Stepik

Download now and try it for free on your own tasks!

Download for Windows
10 15 free credits • No credit card requiredVersion: Loading…

No installation - download and run. No registration - sign in with Google.