python格式化输出的三种形式

法一:

list_a = [1, 2, 3]
str_b = 'aaa'
string = "There are two contents:%s, %s" % (list_a, str_b)
print(string)
# 输出:
# There are two contents:[1, 2, 3], aaa

法二:

list_a = [1, 2, 3]
str_b = 'aaa'
string = "There are two contents:{}, {}".format(list_a, str_b)  # {}默认为输出对应变量的全部内容
print(string)
# 输出:
# There are two contents:[1, 2, 3], aaa

# 若变量为列表等可以通过索引取值的数据,可以在花括号里面加入索引
string = "There are two contents:{[2]}, {}".format(list_a, str_b)
print(string)
# 输出:
# There are two contents:3, aaa
# 可以看出,此时list_a输出的是索引2位置的数据

法三:

list_a = [1, 2, 3]
str_b = 'ccc'
string = f"There are two contents:{list_a}, {str_b}"
print(string)
# 输出:
# There are two contents:[1, 2, 3], ccc
原文地址:https://www.cnblogs.com/jaysonteng/p/12157319.html