pythonpython-3.xstringliststring-formatting

Learning python Python TypeError: not enough arguments for format string


I am new to python and trying to learn the basic

just learned how to format string today and couldn't figure out what is wrong with this line of codes

data = ["Zac", "poor fella", 100]
format_string = "%s is a %s, his bank account has $%s."
print(format_string % data)

my goal is trying to print out "John is a poor fella, his bank account has $100."

But when I run it it came back with "Python TypeError: not enough arguments for format string"

Please let me know what I did wrong, thanks!

I tried putting parentheses in between format_string and data


Solution

  • You have to use tuple instead of list and use % to format the string:

    data = ["Zac", "poor fella", 100]
    print("%s is a %s, his bank account has $%s." % tuple(data))
    

    Result: Zac is a poor fella, his bank account has $100.

    You could use modern f-string formatting:

    data = ["Zac", "poor fella", 100]
    print(f"{data[0]} is a {data[1]}, his bank account has ${data[2]}.")