Why does adding a trailing comma after a variable name make it a tuple?

后端 未结 7 2191
时光说笑
时光说笑 2020-11-22 15:43

I want to know that why adding a trailing comma after a variable name (in this case a string) makes it a tuple. i.e.

>>> abc = \'mystr         


        
7条回答
  •  感情败类
    2020-11-22 16:02

    In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.

    You can do multiple assignments with this similar syntax:

    x,y = 20,50
    

    Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.

    print 'hello',
    print 'world'
    

    result:

    hello world
    

提交回复
热议问题