问题
So what's wrong with this string? I'm not able to figure out why it says there's not enough arguments for format string. I'm new to Python and just figuring things out.
Edit: This is not the same as the other question suggested. The other is trying to do some crazy array stuff that I am not even getting into. I just need to understand the basic concept of tuples and how string formatting works.
data = ["John", 23, "United States", "United Kingdom"]
format_string = "Your name is %s and you are %s years old. You were born in %s and are now living in %s."
print(format_string %data)
Is it because I do not have enought "strings" inside? How do I have a single list with strings and numbers? For example, a JSON list.
回答1:
If you pass the list in as a tuple, it should work just fine.
data = ["John", 23, "United States", "United Kingdom"]
format_string = "Your name is %s and you are %s years old. You were born in %s and are now living in %s."
print(format_string % tuple(data))
回答2:
The right operand of str.__mod__ must either be a tuple or a single value. Since it is not a tuple it is being interpreted as a single value whereas the format string requires a 4-tuple. Either convert data to a tuple or make it a tuple in the first place.
来源:https://stackoverflow.com/questions/43708139/python-string-formatting-type-error-not-enough-arguments-for-format-string