Python タプルを文字列に変換

というような文字のタプルを持っています。

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

のような文字列に変換するにはどうすればよいでしょうか?

'abcdgxre'
ソリューション

str.join`]1を使用します。

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

>>>
解説 (2)

ここでは、joinの簡単な使い方をご紹介します。

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

これは効く。

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

生じます。

'abcdgxre'

また、コンマのような区切り文字を使うと、次のようになります。

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

を使うことで

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