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.
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))
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.