Python之字符串格式化(format)

# 普通調用
"{0}, {1} and {2}".format('spam', 'ham', 'eggs') # 基於位置的調用
"{motto} and {pork}".format(motto = 'spam', pork = 'ham') # 基於Key的調用
"{motto} and {0}".format('ham', motto = 'spam') # 混合調用
# 添加鍵 屬性 偏移量 (import sys)
"my {1[spam]} runs {0.platform}".format(sys, {'spam':'laptop'}) # 基於位置的鍵和屬性
"{config[spam]} {sys.platform}".format(sys = sys, config = {'spam':'laptop'}) # 基於Key的鍵和屬性

"first = {0[0]}, second = {0[1]}".format(['A', 'B', 'C']) # 基於位置的偏移量spa

相關文章
相關標籤/搜索