Several ways to format strings in Python

name = "毛毛"
print("%s,今天你开心嘛!" % name)  # ①
print("{0},今天你开心嘛".format(name))  # ②
print(f"{name},今天你开心嘛!")  # ③

① Similar to the printf() function of the c language, it uses% to pass in the parameter
%, which is essentially a tuple, and multiple parameters can be passed in as a tuple.
② Python2.6 adds a new format string function str.format()
{} The number in {} can be omitted, and the number can choose the order of the incoming parameters.
③ Python3.6 adds a new f-string format Use the format() protocol to format at runtime

Guess you like

Origin blog.csdn.net/qq_46620129/article/details/112058722