Search code examples
pythontuplesstring-concatenation

Print a list of tuples as a string


I have a list of tuples

[
 ('User: ', '<@268326653795500044>, ', 1), 
 ('User: ', '<@381118832963616779>, ', 3), 
 ('User: ', '<@510489897790996492>, ', 1)
]

This is how I create the list of tuples

curs.execute('SELECT userID, strike FROM user WHERE strike != 0')
records = curs.fetchall()

rows = []

for row in records:
  string = 'User: ', f'<@{row[0]}>, ', row[1]
  rows.append(string)
print(rows)

That I want to print as a string: String: User: <@268326653795500044>, 1


Solution

  • data = [('User: ', '<@268326653795500044>, ', 1), 
    ('User: ','<@381118832963616779>, ', 3), 
    ('User: ', '<@510489897790996492>, ', 1)]
    
    
    for i in data:
        print(f"{' '.join(map(str,i))}")
    

    You could just use .join() as the comments pointed out. You do have ints in your tuple so they would have to be converted to strings to then join with the other string values in your tuple.

    Ouput:

    User:  <@268326653795500044>,  1
    User:  <@381118832963616779>,  3
    User:  <@510489897790996492>,  1