Python Forum
how to convert tuple value into string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to convert tuple value into string
#1
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'
Yoriz write Oct-06-2022, 05:24 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You have to convert the items in the tuple separately.
your_string = f"{str(your_tuple[0])} {str(your_tuple[1])}"
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,935 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Convert string to float problem vasik006 8 3,426 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,556 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,442 Apr-12-2022, 08:31 PM
Last Post: buran
  convert a named tuple to a dictionary Skaperen 13 3,564 Mar-31-2022, 07:13 PM
Last Post: Skaperen
  Convert string to int Frankduc 8 2,496 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,474 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,362 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,483 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,418 Apr-13-2021, 07:50 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020