Python Forum
how to convert tuple value into string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to convert tuple value into string (/thread-38378.html)



how to convert tuple value into string - mg24 - Oct-05-2022

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'



RE: how to convert tuple value into string - XavierPlatinum - Oct-06-2022

You have to convert the items in the tuple separately.
your_string = f"{str(your_tuple[0])} {str(your_tuple[1])}"



RE: how to convert tuple value into string - DeaD_EyE - Oct-06-2022

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/sqlite3.html#sqlite3.Cursor.fetchone

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