如何访问动态字符串的长度,作为同一打印方法中的print参数发送?

时间:2014-05-12 05:22:00

标签: python

有人可能急于将字符串发送到print方法,其中包含一些动态参数。假设应该通过format方法打印非常字符串参数的最终长度。这个目标的必要方法是什么?

code = int(input("Get the code: "))
name = "Anonymous"
number = 7


print("{0}'s number is {1} and the code is {2}. The final length of the string is        {3}".format(name,num,code,?))
# ? = length of {0}'s number is {1} and the code is {2}. The final length of the string is {3} 

1 个答案:

答案 0 :(得分:1)

使用len()

code = int(input("Get the code: "))
name = "Anonymous"
number = 7
print("{0}'s length is {1}".format("{0}'s number is {1} and the code is {2}. The final length of the string is {3}".format(name,number,code,len(name+str(number)+str(code))), len("{0}'s number is {1} and the code is {2}. The final length of the string is {3}".format(name,number,code,len(name+str(number)+str(code)))))