2

I am printing the list variable as:

lst=("Python",)*3
print(lst)
lst=("Python")*3
print(lst)

and the output is

('Python', 'Python', 'Python')
PythonPythonPython

Definitely the output is different due to the comma(,) used in the first print statement. But the first statement does not have two values either.

Can someone describe the technical reason behind this?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Babulal
  • 349
  • 3
  • 11

1 Answers1

2

TL;DR:

A trailing , creates a tuple

Tuples:

"Python",

is a tuple of length 1, so

lst=("Python",)*3

Is a tuple of length 3:

('Python', 'Python', 'Python')

Strings:

("Python")

is a string, and thus:

lst=("Python")*3

is a string that is repeated three times:

PythonPythonPython
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135