如何在python输出中使用多个%s?
TEXT = 'Hi, your first name is %s' %Fname
这很好但是......
TEXT = 'Hi, your first name is %s and your last name is %s' %Fname %Lname
我收到错误
TypeError: not enough arguments for format string
答案 0 :(得分:21)
使用元组:
TEXT = 'Hi, your first name is %s and your last name is %s' % (Fname, Lname)
更好:使用str.format(*args, **kwargs)
。
"Hi, your first name is {0} and your last name is {1}".format("foo", "bar")
答案 1 :(得分:1)
你需要使用元组:
TEXT = 'Hi, your first name is %s', % (Fname)
TEXT = 'Hi, your first name is %s and your last name is %s' % (Fname, Lname)
另外,请考虑使用format()
。
答案 2 :(得分:0)
TEXT = 'Hi, your first name is %s and your last name is %s' %(Fname, Lname)
或
TEXT ='Hi, your first name is {} and your last name is {}'.format(Fname, Lname)
答案 3 :(得分:0)
我喜欢使用字符串格式。
所以
TEXT = 'Hi, your first name is {} and your last name is {}'.format(Fname, Lname)
答案 4 :(得分:0)
将所有变量放在元组中,如下所示:
TEXT = 'Hi, your first name is %s and your last name is %s.' %(Fname,Lname)