Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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."

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]}.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading