Python初学者必须吃透这3个格式化输出形式

在这里插入图片描述

说到Python,相信身边有很多的朋友都在接触了,今天就给大家带来Python初学者必须吃透的3个格式化输出形式。

方法一:
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

以上就是关于Python初学者必须吃透的3个格式化输出形式了,大家可以参考上述方法进行格式化输出。

文章部分内容源于网络,联系侵删*
文章转自:http://http.taiyangruanjian.com/news/78974.html

猜你喜欢

转载自blog.csdn.net/zhimaHTTP/article/details/113697645