'Python, remove comma from integer in tuple

how can I remove the comma from an integer in a tuple?

mycursor.execute("SELECT score FROM highscore WHERE userID=4")
highscore = mycursor.fetchall()

for x in highscore:
    print(x)

I'm fetching the numbers from a db, and this is my output

Output:
(324,)
(442,)
(100,)

Can anyone tell my how I can remove the comma in the end of the integers? I would appreciate any help:)



Solution 1:[1]

You're printing the tuple. To access the value just do

for x in highscore:
    print(x[0])

or

for (x,) in highscore:
    print(x)

Solution 2:[2]

Long story short... you can't.

That comma is used to differentiate a tuple from an int declared using the parenthesis notation, as in example:

# This is an int
a = (
  2
)

# This is a tuple
b = (
  2,
)

If you don't want a comma, use a list or a set, or just a single int. But a tuple with a single element inside will always have the trailing comma.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Alexander Santos