print()打印中比%格式符更方便的一個打印方式

有沒有小夥伴到現在還在用以下的%d %f %s…等方式打印

age = int(input("Please input your age:"))
name = input("Please input your name:")
print("Ok,your name is %s, and your age is %d." % (name, age))

當然,也不是說這種方式不能用,但是我們有一種更加直觀並且方便的用法——print(f"").
以下是改進後的代碼

age = int(input("Please input your age:"))
name = input("Please input your name:")
print(f"Ok,your name is {name}, and your age is {age}." )

這樣寫的話是不是比較方便,但一定要記住引號前的 f 一定不能忘記。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章