data=("Lerbron","James",34)
format_str="This is %s %s. His age is %s"
print(format_str %data) #最后34 应该是%d
str1="We are family."
print ("the length of str1 is %d" %len(str1)) # %d
name='James'
age=34
job='player'
if name=='James' and age==34:
print( "%s is %s whose age is %d" %(name,job,age))
print("sum = %d" %(sum))
a=2
b="test"
c=str(a)+b
d="1111"
e=a+int(d)
#How to print multiply values
print ("c is %s,e is %i" % (c,e))
#这个其实在前面也有所提及
上面算是比较常规化的写法:
下面看到有一些特殊的写法
print(f'requests failed {i} time')
格式化的字符串文字前缀为’f’和接受的格式字符串相似str.format()。它们包含由花括号包围的替换区域。替换字段是表达式,在运行时进行评估,然后使用format()协议进行格式化。
formatted string literals, 以 f 开头,包含的{}表达式在程序运行时会被表达式的值代替
所有在这里{i}会被上文中的i所替换
在这里变量{name},以及变量{num} 也会被上文中的变量替换;去电f,则会当字符串处理。
原始参考链接1