在 Python 中使用字符串格式打印元组 [英] Printing tuple with string formatting in Python

查看:44
本文介绍了在 Python 中使用字符串格式打印元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, i have this problem. I got tuple (1,2,3) which i should print with string formatting. eg.

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

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

and this should print tuple representation with brackets, like

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

This is a tuple (1,2,3)

但我得到 TypeError: 不是所有的参数都在字符串格式化过程中转换.

我到底怎么能做到这一点?有点迷失在这里,所以如果你们能指出我正确的方向:)

How in the world am I able to do this? Kinda lost here so if you guys could point me to a right direction :)

推荐答案

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

制作一个以感兴趣的元组为唯一项目的单例元组,即 (thetuple,) 部分,是这里的关键.

Making a singleton tuple with the tuple of interest as the only item, i.e. the (thetuple,) part, is the key bit here.

这篇关于在 Python 中使用字符串格式打印元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆