Python convert tuple to string

后端 未结 4 507
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 15:01

I have a tuple of characters like such:

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

How do I convert it to a string so that it

相关标签:
4条回答
  • 2020-11-27 15:30

    Easiest way would be to use join like this:

    >>> myTuple = ['h','e','l','l','o']
    >>> ''.join(myTuple)
    'hello'
    

    This works because your delimiter is essentially nothing, not even a blank space: ''.

    0 讨论(0)
  • 2020-11-27 15:35

    This works:

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

    It will produce:

    'abcdgxre'
    

    You can also use a delimiter like a comma to produce:

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

    By using:

    ','.join(('a', 'b', 'c', 'd', 'g', 'x', 'r', 'e'))
    
    0 讨论(0)
  • 2020-11-27 15:41

    here is an easy way to use join.

    ''.join(('a', 'b', 'c', 'd', 'g', 'x', 'r', 'e'))
    
    0 讨论(0)
  • 2020-11-27 15:51

    Use 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.
    
    >>>
    
    0 讨论(0)
提交回复
热议问题