Home > Mobile >  Python, remove comma from integer in tuple
Python, remove comma from integer in tuple

Time:04-29

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

CodePudding user response:

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)

CodePudding user response:

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.

  • Related