format()方法就是格式化指定的值,然后再将其插入字符串的占位符内
占位符用大括号{}来定义哈,可以使用命名索引{sws}、编号索引{0}、甚至空的占位符{}来标识占位符。
语法就是
string.format(value1, value2...)
话不多说,来看代码实在点
sws_1 = "My name is {name}, I'm {age}".format(name="Slowsnail", age=20)
sws_2 = "My name is {0}, I'm {1}".format("Slowsnail", 20)
sws_3 = "My name is {}, I'm {}".format("Slowsnail", 20)
print(sws_1, '\n', sws_2, '\n', sws_3)
结果肯定大家都已经想到啦
My name is Slowsnail, I'm 20
My name is Slowsnail, I'm 20
My name is Slowsnail, I'm 20
更多占位符的使用技巧,推荐大家看看https://www.w3school.com.cn/python/ref_string_format.asp