Thursday, October 9, 2014

Convert string to array in python

Example:
   string1 = 'It is so cold outside'
   array = string1.split(' ')

Check array:
   print array
   ['It', 'is', 'so', 'cold', 'outside']

Convert array to string:
   string2 = ' '.join(array)

Check string2:
   print string2
   'It is so cold outside'

Just for fun:
   string2 = '-'.join(array)

Check string2:
   print string2
   'It-is-so-cold-outside'

No comments: