一、字符串前加"f"1. %可以使用 % 格式化字符串。c = (250, 250) # 使用 % 格式化 s1 = "坐标为:%s" % c # TypeError: not all arguments converted during string formatting s1 = "坐标为:%s" % (c,) # '坐标为:(250, 250)' # 使用 format 格式化 s2 = "坐标为:{}".format(c) # '坐标为:(250, 250)'1.2.3.4.5.6.7.2.formatPython 2.6 引入 format 格式化字符串的方式。str.format() 是对 %-formatting 的改进,替换字段使用大括号 {} 标记。"Hello, {}. You are {}.".format(name, age)1.可以通过引用索引来改变引入顺序:age = 100 name = 'Hider' "Hello {1}.You are {0}-{0}."
Xinbo