有人可能急于将字符串发送到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}
答案 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)))))