I am trying to understand the format() function in python while completing a codewars challenge. My solution was a lot less elegant as I used if statements to insert the phone number formatting at specific positions.
The top answer was:
def create_phone_number(n):
return "({}{}{}) {}{}{}-{}{}{}{}".format(*n)
I understand that format() will replace each curly braces with whatever you enter as a parameter and the list length cannot be less than the number of braces. How does the format() function know to traverse the list at each position starting at n[0] and replace the empty curly braces when no variable is put in the string it is formatting?