Main.StringsAndLists History
Hide minor edits - Show changes to markup
February 16, 2010, at 08:47 AM MST
by -
Added lines 1-41:
(:source lang=python:)
""" Making lists out of strings and strings out of lists http://openbookproject.net/thinkcs/python/english2e/ch09.html#strings-and-lists """
- Python has a command called list that takes a sequence type as an argument
- and creates a list out of its elements.
list("Crunchy Frog")
- given a list of characters we can join them together to form a string:
import string char_list = list("Frog") print char_list print string.join(char_list, '')
- note the second parameter of string.join: it determines which character
- to insert between each element of the list.
- this way we can easily create comma delmited output:
name_list = ['Asa', 'Mark', 'Adam'] print string.join(name_list, ',')
print string.join(name_list, '**')
- the opposite action is possible using string.split:
import string song = "The rain in Spain..." print string.split(song)
- this form of string.split splits the string on any whitespace character
- string.split has an optional argument which specifies the delimiter.
- suppose we are given a comma delimited string such as:
comma_delimited = 'Asa,Mark,Adam'
(:sourceend:)
