Python Forum

Full Version: how to convert tuple value into string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

how to convert tuple value into a string.
after running sql query I get output in tuple.

want to convert it into string.

sql_data = cursor.fetchall()[0]
sql_data = ('AUS',)    # tuple value , expected only 'AUS'
Expected output
Output:
sql_string = 'AUS'
You have to convert the items in the tuple separately.
your_string = f"{str(your_tuple[0])} {str(your_tuple[1])}"
The method fetchall returns a list with results. If there is no result, the list is empty.
For each result in the list, you have to access the field.

What you could do:
sql_data = cursor.fetchall()[0][0]
# acessing the first result, then the first element of the result
If you want only one result, then use the right method.
https://docs.python.org/3/library/sqlite...r.fetchone

If fetchone returns a None, then you have no result.
If result is a tuple, then you got your result.