2013-10-28 60 views
45

我有人物像這樣一個元組:Python的轉換元組串

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

如何將其轉換爲一個字符串,它是這樣的:

'abcdgxre' 
+8

' ''。加入(( 'A', 'B', 'C', 'd', 'G', 'X', 'R', 'E'))' –

+0

嘗試此還有'reduce(add,('a','b','c','d'))' –

+0

什麼是這個例子中的'add' @GrijeshChauhan? – Steve

回答

82

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

>>> 
+16

如果元組包含數字。嘗試tup =(3,None,None,None,None,1406836313736) – Raj

+30

對於數字你可以試試這個:'''.join(map(str,tup))' – Mohammad

18

這裏是使用連接的簡單方法。

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