Tuesday, October 30, 2012

Python programming Day 05

To convert from a string to a list of characters

>>> s = 'spam'
>>> t = list(s)
>>> print t
['s', 'p', 'a', 'm']

To break a string into words

 >>> s = 'pining for the fjords'
>>> t = s.split()
>>> print t
['pining', 'for', 'the', 'fjords']

To convert all elements in a list to a string

>>> t = ['pining', 'for', 'the', 'fjords']
>>> delimiter = ' ' # the delimiter is a space character, so join puts a space between words
>>> delimiter.join(t)
'pining for the fjords'


References
Allen B. Downey, Think Python: How to Think Like a Computer Scientist,
http://www.greenteapress.com/thinkpython/html/index.html

No comments:

Post a Comment

Mounting USB drives in Windows Subsystem for Linux

Windows Subsystem for Linux can use (mount): SD card USB drives CD drives (CDFS) Network drives UNC paths Local storage / drives Drives form...