Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple to string

Tags:

python

I have a tuple.

tst = ([['name', u'bob-21'], ['name', u'john-28']], True)

And I want to convert it to a string..

print tst2
"([['name', u'bob-21'], ['name', u'john-28']], True)"

what is a good way to do this?

Thanks!

like image 686
Dais Avatar asked Oct 07 '10 23:10

Dais


People also ask

How do you convert a tuple to a string?

Create an empty string and using a for loop iterate through the elements of the tuple and keep on adding each element to the empty string. In this way, the tuple is converted to a string. It is one of the simplest and the easiest approaches to convert a tuple to a string in Python.

How do you return a tuple to a string in Python?

Use the str. join() Function to Convert Tuple to String in Python. The join() function, as its name suggests, is used to return a string that contains all the elements of sequence joined by an str separator. We use the join() function to add all the characters in the input tuple and then convert it to string.

Is a tuple a string?

Tuple they are immutable like strings and sequence like lists. They are used to store data just like list, just like string you cannot update or edit the tuple to change it you have to create a new one just like strings. Tuples can be created using parenthesis () and data is inserted using comas.

How do you convert a tuple to a variable in Python?

In python tuples can be unpacked using a function in function tuple is passed and in function values are unpacked into normal variable.


1 Answers

tst2 = str(tst)

E.g.:

>>> tst = ([['name', u'bob-21'], ['name', u'john-28']], True)
>>> tst2 = str(tst)
>>> print tst2
([['name', u'bob-21'], ['name', u'john-28']], True)
>>> repr(tst2)
'"([[\'name\', u\'bob-21\'], [\'name\', u\'john-28\']], True)"'
like image 113
mechanical_meat Avatar answered Sep 30 '22 18:09

mechanical_meat