%对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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短