%对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
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬