Answers:
请注意,该%
语法已过时。使用str.format
,它更简单易读:
t = 1,2,3
print 'This is a tuple {0}'.format(t)
%
过时了,但str.format
无论如何我现在总是盲目地对待你。没关系,我对完全限定词是什么感兴趣,因为简单{0}
的不是完全限定词,而只是位置指示符。对于int
,我称之为完全限定符{0:d}
(或{3:d}
例如,如果要打印的int出现在方法的第4个位置str.format
)。我尝试使用{0:s}
限定符打印一个元组,但是它不起作用。那么,像元组这样的完全限定词是什么?
str
,可以说{!s}
,但这是默认设置,因此没有必要。如果您想repr
改用,则可以这样做{!r}
。与with不同的是%
,不需要说d
整数。
%
也不是完全过时的。实际上,pylint
如果您.format
在logging
模块中使用以下语法,甚至会抱怨: stackoverflow.com/a/34634301/534238
上面给出的许多答案都是正确的。正确的方法是:
>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)
但是,关于'%'
String运算符是否已过时存在争议。正如许多人指出的那样,这绝对不是过时的,因为'%'
String运算符更容易将String语句与列表数据组合在一起。
例:
>>> tup = (1,2,3)
>>> print "First: %d, Second: %d, Third: %d" % tup
First: 1, Second: 2, Third: 3
但是,使用该.format()
函数将得到一个冗长的语句。
例:
>>> tup = (1,2,3)
>>> print "First: %d, Second: %d, Third: %d" % tup
>>> print 'First: {}, Second: {}, Third: {}'.format(1,2,3)
>>> print 'First: {0[0]}, Second: {0[1]}, Third: {0[2]}'.format(tup)
First: 1, Second: 2, Third: 3
First: 1, Second: 2, Third: 3
First: 1, Second: 2, Third: 3
进一步,'%'
串操作者也为我们验证数据类型如有用%s
,%d
,%i
,而.format()只支持两个转换标志:'!s'
和'!r'
。
.format
:tup = (1,2,3); print('First: {}, Second: {}, Third: {}'.format(*tup))
。
>>> tup = (1, 2, 3)
>>> print "Here it is: %s" % (tup,)
Here it is: (1, 2, 3)
>>>
请注意,这(tup,)
是一个包含元组的元组。外部元组是%运算符的参数。内部元组是其内容,它实际上是打印的。
(tup)
是括号中的表达式,计算时结果为tup
。
(tup,)
后跟逗号的是一个元组,它tup
仅包含一个成员。
t = (1, 2, 3)
# the comma (,) concatenates the strings and adds a space
print "this is a tuple", (t)
# format is the most flexible way to do string formatting
print "this is a tuple {0}".format(t)
# classic string formatting
# I use it only when working with older Python versions
print "this is a tuple %s" % repr(t)
print "this is a tuple %s" % str(t)
即使这个问题已经很老了并且有很多不同的答案,我仍然想添加恕我直言,这是最“ pythonic”的,也是可读/简洁的答案。
由于tuple
锑已经正确地显示了常规打印方法,因此这是用于分别打印元组中的每个元素的附加功能,因为方嘉俊(Fong Kah Chun)已正确显示了该方法。%s
语法。
有趣的是它在评论被只提到,但使用一个星号操作来解压缩所述元组产生完全的灵活性和可读性使用str.format
方法分别打印元组元素时。
tup = (1, 2, 3)
print('Element(s) of the tuple: One {0}, two {1}, three {2}'.format(*tup))
这也避免了在打印单元素元组时打印尾随逗号,这由Jacob CUI绕过replace
。(即使恕我直言,如果要在打印时保留类型表示形式,则尾部逗号表示形式也是正确的):
tup = (1, )
print('Element(s) of the tuple: One {0}'.format(*tup))
我认为最好的方法是:
t = (1,2,3)
print "This is a tuple: %s" % str(t)
如果您熟悉printf样式格式,则Python支持其自己的版本。在Python中,这是通过将“%”运算符应用于字符串(模运算符的重载)来完成的,该运算符采用任何字符串并对其应用printf样式格式。
在我们的例子中,我们告诉它打印“ This is a tuple:”,然后添加一个字符串“%s”,对于实际的字符串,我们传入该tuple的字符串表示形式(通过调用str( t))。
如果您不熟悉printf样式格式,我强烈建议您学习,因为它非常标准。大多数语言都以某种方式支持它。
请注意,如果元组只有一项,则会在末尾添加逗号。例如:
t = (1,)
print 'this is a tuple {}'.format(t)
您会得到:
'this is a tuple (1,)'
在某些情况下,例如,您想获取要在mysql查询字符串中使用的带引号的列表,例如
SELECT name FROM students WHERE name IN ('Tom', 'Jerry');
您需要考虑在格式化后使用replace(',)',')')删除尾部逗号,因为元组可能只有1个项目,例如('Tom',),因此需要删除尾部逗号:
query_string = 'SELECT name FROM students WHERE name IN {}'.format(t).replace(',)', ')')
请建议您是否有适当的方法在输出中删除此逗号。
format()
在数据库查询中使用。每个库中都有适用的方法。
format()
从用户收集的值可能会导致SQL注入。在将输入值放入查询之前验证输入值更安全。有关更多信息,请参见以下准则:dev.mysql.com/doc/connector-python/en/…您还可以将参数值转换为数据库类型的字符串表示形式,然后将其粘贴到查询字符串中。看一下initd.org/psycopg/docs/usage.html,其中有一个带%
运算符的反例和一个带execute()
方法的解决方案,其中在参数args中传递了查询参数。
试试这个来获得答案:
>>>d = ('1', '2')
>>> print("Value: %s" %(d))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
如果我们在()中只放入一个元组,它本身就会构成一个元组:
>>> (d)
('1', '2')
这意味着上面的打印语句看起来像:print(“ Value:%s”%('1','2'))这是一个错误!
因此:
>>> (d,)
(('1', '2'),)
>>>
上面的内容将正确输入打印的参数。
您也可以尝试这个。
tup = (1,2,3)
print("this is a tuple {something}".format(something=tup))
您不能使用%something
与(tup)
包装,并用元组拆包概念只是因为。
print(....)
。