在Python中使用字符串格式打印元组

vet*_*uri 116 python

所以,我有这个问题.我得到了元组(1,2,3),我应该用字符串格式打印.例如.

tup = (1,2,3)
print "this is a tuple %something" % (tup)
Run Code Online (Sandbox Code Playgroud)

这应该用括号打印元组表示,比如

这是一个元组(1,2,3)

但我得到了TypeError: not all arguments converted during string formatting.

我能在世界上做到这一点吗?有点失去了这里,如果你们可以指出我正确的方向:)

Ale*_*lli 187

>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

使用感兴趣的元组作为唯一项目(即(thetuple,)部分)创建单例元组是这里的关键位.


Ant*_*ony 49

请注意,%语法已过时.使用str.format,更简单,更易读:

t = 1,2,3
print 'This is a tuple {0}'.format(t)
Run Code Online (Sandbox Code Playgroud)

  • 我有点像%语法,因为它比新格式更简洁,也非常类似于我知道和喜欢的c字符串格式 (2认同)

小智 31

上面给出的许多答案是正确的.正确的方法是:

>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

但是,如果'%'String运算符已过时,则存在争议.正如许多人所指出的那样,它绝对不会过时,因为'%'String运算符更容易将String语句与列表数据相结合.

例:

>>> tup = (1,2,3)
>>> print "First: %d, Second: %d, Third: %d" % tup
First: 1, Second: 2, Third: 3
Run Code Online (Sandbox Code Playgroud)

但是,使用该.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
Run Code Online (Sandbox Code Playgroud)

进一步,'%'串操作者也为我们验证数据类型如有用%s,%d,%i,而.format()只支持两个转换标志:'!s''!r'.

  • “星号解压”元组与`.format`配合使用:`tup =(1,2,3); print('First:{},Second:{},Third:{}'。format(* tup))`。 (4认同)
  • 您还可以指定每个元素的格式。'{:d}{:s}'.format(1, '2') (2认同)

Vin*_*jip 23

>>> tup = (1, 2, 3)
>>> print "Here it is: %s" % (tup,)
Here it is: (1, 2, 3)
>>>
Run Code Online (Sandbox Code Playgroud)

注意,它(tup,)是一个包含元组的元组.外部元组是%运算符的参数.内部元组是其内容,实际上是打印出来的.

(tup)是括号中的表达式,当计算结果时tup.

(tup,)尾随逗号是一个元组,tup它只包含成员.


Sco*_*y1- 9

尽管这个问题已经很老了并且有很多不同的答案,但我仍然想添加最“pythonic”并且可读/简洁的答案。

由于tupleAntimony 已经正确显示了一般的打印方法,这是一个单独打印元组中每个元素的补充,正如 Fong Kah Chun 用%s语法正确显示的那样。

有趣的是,它只在评论中提到过,但是使用星号运算符来解包元组会在单独打印元组元素时使用该str.format方法产生完全的灵活性和可读性。

tup = (1, 2, 3)
print('Element(s) of the tuple: One {0}, two {1}, three {2}'.format(*tup))
Run Code Online (Sandbox Code Playgroud)

这也避免了在打印单元素元组时打印尾随逗号,正如 Jacob CUI 使用replace. (即使恕我直言,如果要在打印时保留类型表示,尾随逗号表示是正确的):

tup = (1, )
print('Element(s) of the tuple: One {0}'.format(*tup))
Run Code Online (Sandbox Code Playgroud)


Ton*_*has 9

除了其他答案中提出的方法之外,从 Python 3.6 开始,我们还可以使用文字字符串插值(f-strings):

>>> tup = (1,2,3)
>>> print(f'this is a tuple: {tup}.')
this is a tuple: (1, 2, 3).
Run Code Online (Sandbox Code Playgroud)


TM.*_*TM. 8

这不使用字符串格式,但您应该能够:

print 'this is a tuple ', (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

如果你真的想使用字符串格式:

print 'this is a tuple %s' % str((1, 2, 3))
# or
print 'this is a tuple %s' % ((1, 2, 3),)
Run Code Online (Sandbox Code Playgroud)

请注意,这假设您使用的是早于3.0的Python版本.


Est*_*ber 8

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)
Run Code Online (Sandbox Code Playgroud)


Eda*_*aor 5

我认为最好的方法是:

t = (1,2,3)

print "This is a tuple: %s" % str(t)
Run Code Online (Sandbox Code Playgroud)

如果您熟悉printf样式格式,那么 Python 支持它自己的版本。在 Python 中,这是使用应用于字符串的“%”运算符(模运算符的重载)完成的,它接受任何字符串并对其应用 printf 样式的格式。

在我们的例子中,我们告诉它打印“这是一个元组:”,然后添加一个字符串“%s”,对于实际的字符串,我们传入一个元组的字符串表示(通过调用 str( t))。

如果您不熟悉 printf 样式格式,我强烈建议您学习,因为它非常标准。大多数语言都以一种或另一种方式支持它。


Jac*_*CUI 5

请注意,如果元组只有一项,则会添加尾随逗号。例如:

t = (1,)
print 'this is a tuple {}'.format(t)
Run Code Online (Sandbox Code Playgroud)

你会得到:

'this is a tuple (1,)'
Run Code Online (Sandbox Code Playgroud)

在某些情况下,例如,您想要获取要在 mysql 查询字符串中使用的引用列表,例如

SELECT name FROM students WHERE name IN ('Tom', 'Jerry');
Run Code Online (Sandbox Code Playgroud)

您需要考虑在格式化后使用replace(',)', ')')删除尾部逗号,因为元组可能只有1项,例如('Tom',),因此需要删除尾部逗号:

query_string = 'SELECT name FROM students WHERE name IN {}'.format(t).replace(',)', ')')
Run Code Online (Sandbox Code Playgroud)

请建议您是否有适当的方法来删除输出中的这个逗号。