Python将元组转换为字符串

新手上路,请多包涵

我有一个这样的字符元组:

 ('a', 'b', 'c', 'd', 'g', 'x', 'r', 'e')

如何将其转换为字符串,使其类似于:

 'abcdgxre'

原文由 intel3 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 320
2 个回答

使用 str.join

 >>> tup = ('a', 'b', 'c', 'd', 'g', 'x', 'r', 'e')
>>> ''.join(tup)
'abcdgxre'
>>>
>>> help(str.join)
Help on method_descriptor:

join(...)
    S.join(iterable) -> str

    Return a string which is the concatenation of the strings in the
    iterable.  The separator between elements is S.

>>>

原文由 user2555451 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是使用 join 的简单方法。

 ''.join(('a', 'b', 'c', 'd', 'g', 'x', 'r', 'e'))

原文由 Back2Basics 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题