%对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 生日广州男生祝福语简短