-2

I am having a string like ...

"1" "2" "3" "4" " " "text1" "text2" "text3" "6" "first && second && third.." " " " 7 " "         8"..  

What i Intended to achieve is...

 1,2,3,4,,text1,text2,text3,6,first && second && third..,,7,8,..  

Can any one Help me with this ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sravan K Ghantasala
  • 1,058
  • 8
  • 14

1 Answers1

1

If you have a list of strings, then strip each element of whitespace and use str.join() to join these together with a delimiter:

','.join([s.strip() for s in list_of_strings])

Demo:

>>> list_of_strings = ["1", "2", "3", "4", " ", "text1", "text2", "text3", "6", "first && second && third..", " ", " 7 ", " "]
>>> ','.join([s.strip() for s in list_of_strings])
'1,2,3,4,,text1,text2,text3,6,first && second && third..,,7,'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343