Skip to content
Advertisement

python tuple ending with comma difference

What is the difference between

("a")

and

("a",)

I noticed that for example mysql wrapper parameters formatting doesn’t work with the first case, it must end with comma.

cursorA.execute(query, (url,))

Advertisement

Answer

if you write only one element in parentheses (), the parentheses () are ignored and not considered a tuple.

x = ("a")

print(type(x))

output: str

to generate a one-element tuple, a comma , is needed at the end.

x = ("a", )

print(type(x))

ouput : tuple

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement