1

I have been attempting to parse variables from a list into a function separately. I don't know how to parse each value from the list into the .format() function. The solution is probably pretty obvious but I can't seem to find it. Here is a simplified version of the code:

variables = ["a", "b", "c", "d"]

def format(variables):
    string = "{:^4}" * len(variables)

    #don't know how to do
    return string.format(variables_from_list)

Ideally, the code would work the same as below:

variables = ["a", "b", "c", "d"]

def format(variables):
    string = "{:^4}" * len(variables)

    print(string.format("a", "b", "c", "d"))

Any help is appreciated!

S. Allen
  • 171
  • 1
  • 10
  • 2
    Use "splat" arg unpacking: `.format(*variables)` – PM 2Ring Aug 12 '18 at 09:32
  • 1
    Please see the section starting "Another usage of the `*l` idiom is to unpack argument lists when calling a function." in the accepted answer of the linked question. – PM 2Ring Aug 12 '18 at 09:37
  • BTW, it's a good idea to avoid using `string` as a variable name because it's the name of a standard module. – PM 2Ring Aug 12 '18 at 09:39
  • This was just an example of the problem and not a sample of my code but thanks anyway. – S. Allen Aug 12 '18 at 11:54

0 Answers0