How to create a tuple with only one element

前端 未结 3 1362
执笔经年
执笔经年 2020-11-22 00:43

In the below example I would expect all the elements to be tuples, why is a tuple converted to a string when it only contains a single string?

>>> a         


        
3条回答
  •  孤城傲影
    2020-11-22 01:23

    Your first two examples are not tuples, they are strings. Single-item tuples require a trailing comma, as in:

    >>> a = [('a',), ('b',), ('c', 'd')]
    >>> a
    [('a',), ('b',), ('c', 'd')]
    

提交回复
热议问题