Tuesday, 1 October 2013

python - first alphabetic character, last, and pull the string in between

python - first alphabetic character, last, and pull the string in between

Strip string and return lowercase version of result
Return a lowercase version of the word with all non-alphabetic symbols
stripped away from the boundaries, but with such symbols remaining in the
interior of a word.
Precondition: wd is a string Postcondition: returns 'stripped' string in
lowercase
Typical examples of this behavior include:
>>> word_strip("don't")
"don't"
>>> word_strip('end.')
'end'
>>> word_strip('jack-o-lantern')
'jack-o-lantern'
>>> word_strip('"Stop!"')
'stop'
>>> word_strip('&%^$*%*%"')
''
>>> word_strip('hello there!!')
'hello there'
Code:
import string
def word_strip(wd):
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(0,len(wd)):
if wd[i] == alpha:
a = wd.find(alpha)
# locate last desirable character
b = wd.endswith(alpha)
# return an intentionally lowercase stripped word
wd = wd[a:b]
wd = wd.lower()
return wd

No comments:

Post a Comment