python tuple ending with comma difference

Tags: ,



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,))

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



Source: stackoverflow