Skip to content Skip to sidebar Skip to footer

How To Convert Single Element Tuple Into String?

I have this code: import nltk import pypyodbc text = raw_input() token = nltk.word_tokenize(text) //return a list value def search(self, lists): if not self.connected:

Solution 1:

You have to use str.join method.

>>> a = [('tore',), ('ngaming',), ('sittam',)]
>>> " ".join([x[0] for x in a])
'tore ngaming sittam'

Solution 2:

I would just use a for loop. You can loop through the list of tuples and add each element to an empty string variable

x = [('tore'), ('ngaming'), ('sittam')]
z = ""
for i in x:
    z = z + i + " "

print z

After seeing Lafada's answer, I realized that list comprehension and str.join is the best option. Though his answer is better, this is basically what his answer does, just done more manually.


Post a Comment for "How To Convert Single Element Tuple Into String?"