Some string methods along with string replace, find, and where.
Python string methods.
Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Friday August 01st, 2008 01:26 AM
# some examples of string methods:
>>> S = 'hello World!'
>>> S = S.capitalize()
>>> S
'Hello World!'
>>>S.endswith('World!')
True
>>> list = dircache.listdir('Pictures')
>>> pictures = {}
>>> i = 0
>>> for x in list:
... if x.endswith('.jpg'):
... pictures[i]=x
... i += 1
>>> S = 'test'
>>> D = '$'
>>> D.join(S)
't$e$s$t'
>>> S = S.replace('Hello','Goodbye')
>>> S
'Goodbye World'
>>> S.split()
['Goodbye', 'World']
>>> S.split('o')
['G', '', 'dbye W', 'rld']
>>> S.replace('o','x',2)
'Gxxdbye World'
# there is also a string module with a string replace
import string
>>> import string
>>> S = 'somestring'
>>> string.replace(S,'s','x')
'xomextring'
>>> S = 'Goodbye World'
>>> where = S.find('od')
>>> where
2
>>> S[2]
'o'
>>> S[2:]
'odbye World'
>>> S[:2]
'Go'
>>> list(S)
['G', 'o', 'o', 'd', 'b', 'y', 'e', ' ', 'W', 'o', 'r', 'l', 'd']
