2024-04-24 16:24:08 发布
网友
假设我有一个字符串:
s = "Tuple: "
和Tuple(存储在名为tup的变量中):
(2, a, 5)
我试图让我的字符串包含值“Tuple:(2,a,5)”。我注意到你不能把它们连起来。有人知道最直接的方法吗?谢谢。
试着加入元组。我们需要使用map(str,tup),因为有些值是整数,join只接受字符串。
s += "(" + ', '.join(map(str,tup)) + ")"
这也适用于:
>>> s = "Tuple: " + str(tup) >>> s "Tuple: (2, 'a', 5)"
>>> tup = (2, "a", 5) >>> s = "Tuple: {}".format(tup) >>> s "Tuple: (2, 'a', 5)"
试着加入元组。我们需要使用map(str,tup),因为有些值是整数,join只接受字符串。
这也适用于:
相关问题 更多 >
编程相关推荐