Python字符串格式-类型错误-格式字符串的参数不足 [英] Python String Formatting - Type Error - Not enough arguments for format string

查看:134
本文介绍了Python字符串格式-类型错误-格式字符串的参数不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么这个字符串怎么了?我无法弄清楚为什么它说没有足够的参数来表示格式字符串.我是Python的新手,只是想出办法.

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.

这与建议的其他问题不同.另一个正在尝试做一些我什至没有想到的疯狂的阵列工作.我只需要了解元组的基本概念以及字符串格式是如何工作的.

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)

是因为内部没有足够的"字符串" 吗?如何获得一个包含字符串和数字的列表?例如,一个JSON列表.

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))

这篇关于Python字符串格式-类型错误-格式字符串的参数不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆