%对Python中的字符串有何作用?
%是字符串格式运算符或插值运算符。给定格式%值(其中format是一个字符串),格式中的%转换规范将替换为零个或多个值元素。效果类似于sprintf()在C语言中使用。例如,
>>> lang = "Python" >>> print "%s is awesome!" % lang Python is awesome
您也可以使用此符号格式化数字。例如,
>>> cost = 128.527 >>> print "The book costs $%.2f at the bookstore" % cost The book costs $128.53 at the bookstore
您还可以使用字典来插值字符串。它们具有一种语法,您需要使用该语法在%和转换字符之间的括号中提供键。例如,
print('%(language)s has %(number)03d quote types.' % {'language': "Python", "number": 2})
Python has 002 quote types.您可以在此处阅读有关字符串格式及其操作符的更多信息:https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短