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

我有个问题。 我得到了 tuple (1,2,3) ,我应该用字符串格式打印它。 例如。

tup = (1,2,3)
print "this is a tuple %something" % (tup)

它应该打印带括号的元组表示,如

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

但我得到的是 TypeError: not all arguments converted during string formatting

我到底是怎么做到的?我有点迷路了,所以如果你们能给我指个正确的方向:)

278306 次浏览

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

print 'this is a tuple ', (1, 2, 3)

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

print 'this is a tuple %s' % str((1, 2, 3))
# or
print 'this is a tuple %s' % ((1, 2, 3),)

注意,这里假设您使用的 Python 版本早于3.0。

>>> # Python 2
>>> thetuple = (1, 2, 3)
>>> print "this is a tuple: %s" % (thetuple,)
this is a tuple: (1, 2, 3)


>>> # Python 3
>>> thetuple = (1, 2, 3)
>>> print(f"this is a tuple: %s" % (thetuple,))
this is a tuple: (1, 2, 3)

这里的关键点是创建一个单元元组,并将感兴趣的元组作为唯一的项目,也就是 (thetuple,)部分。

>>> 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)

我认为最好的方法是:

t = (1,2,3)


print "This is a tuple: %s" % str(t)

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

在我们的示例中,我们告诉它输出“ This is a tuple:”,然后添加一个字符串“% s”,对于实际的字符串,我们传入 tuple 的字符串表示形式(通过调用 str (t))。

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

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

t = 1,2,3
print 'This is a tuple {0}'.format(t)

请注意,如果元组只有一个项目,后面的逗号将被添加。例如:

t = (1,)
print 'this is a tuple {}'.format(t)

你会得到:

'this is a tuple (1,)'

在某些情况下,例如,您希望获得一个引用列表,以便在 mysql 查询字符串中使用,如

SELECT name FROM students WHERE name IN ('Tom', 'Jerry');

您需要考虑在格式化之后删除尾逗号,因为元组可能只有一个类似于(‘ Tom’,)的条目,所以需要删除尾逗号:

query_string = 'SELECT name FROM students WHERE name IN {}'.format(t).replace(',)', ')')

请建议,如果你有体面的方式删除这个逗号在输出。

上面给出的许多答案都是正确的,正确的做法是:

>>> 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)也很有用。格式() 只支持两个转换标志: '!s''!r'

谈话是廉价的,告诉你代码:

>>> tup = (10, 20, 30)
>>> i = 50
>>> print '%d      %s'%(i,tup)
50  (10, 20, 30)
>>> print '%s'%(tup,)
(10, 20, 30)
>>>

试试这个答案:

>>>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

如果我们只在()中放入-one tuple,它就会自己生成一个 tuple:

>>> (d)
('1', '2')

这意味着上面的 print 语句看起来像: Print (“ Value:% s”% (’1’,’2’)) ,这是一个错误!

因此:

>>> (d,)
(('1', '2'),)
>>>

以上将被正确地反馈到印刷的参数。

尽管这个问题已经很老了,有很多不同的答案,我还是想添加一个最“蟒蛇”的回答,同时也是可读/简洁的回答。

由于通用的 tuple打印方法已经正确地显示锑,这是一个额外的打印每个元素在一个元组分开,因为方家俊已经正确地显示与 %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))

巨蟒3

tup = (1,2,3)
print("this is a tuple %s" % str(tup))

你也可以试试这个;

tup = (1,2,3)
print("this is a tuple {something}".format(something=tup))

您不能将 %something(tup)一起使用,仅仅是因为使用 tuple 打包和解包的概念。

除了在其他答案中提出的方法之外,由于 Python 3.6,您还可以使用 字符串插值(f-string) :

>>> tup = (1,2,3)
>>> print(f'this is a tuple {tup}')
this is a tuple (1, 2, 3)

在 python3中使用 f-string 进行快速打印。

tup = (1,2,3)
print(f"this is a tuple {tup}")

这些年来发生了多大的变化,现在你可以这样做:

tup = (1,2,3)
print(f'This is a Tuple {tup}.')

结果: 这是一个 Tuple (1,2,3)。